How i can go through database tables inside my django template using “foreign key”Django foreign key access in save() functiondjango - inlineformset_factory with more than one ForeignKeySaving form data rewrites the same rowShow information of subclass in list_display djangoWhat is wrong with my models.py?Radio buttons in django adminsave() prohibited to prevent data loss due to unsaved related object 'postid'How to expose some specific fields of model_b based on a field of model_a?How to define Mode with generic ForeignKey in DjangoHow to implement update_or_create inside create method of ModelSerializer

How can this triangle figure be modeled/drawn with TikZ?

For the erase-remove idiom, why is the second parameter necessary which points to the end of the container?

On studying Computer Science vs. Software Engineering to become a proficient coder

Why was Endgame Thanos so different than Infinity War Thanos?

Can the sorting of a list be verified without comparing neighbors?

Two researchers want to work on the same extension to my paper. Who to help?

Why doesn't Rocket Lab use a solid stage?

How to slow yourself down (for playing nice with others)

Can I use my laptop, which says 100-240V, in the USA?

Is a vector space automatically spacelike if it has a basis of spacelike vectors?

Meaning of "sib of 20" in a by-patient case description table

Can you book a one-way ticket to the UK on a visa?

use the oversamplling followed by '' decimation method ''to increasee the ADC resolution and not normal averaging

Why didn't Aeroflot Flight 1492 dump its fuel first?

How to pronounce "r" after a "g"?

Was this character’s old age look CGI or make-up?

How are Core iX names like Core i5, i7 related to Haswell, Ivy Bridge?

How to minimise the cost of guessing a number in a high/low guess game?

Why was castling bad for white in this game, and engine strongly prefered trading queens?

A curve pass via points at TiKz

What does i386 mean on macOS Mojave?

Make all the squares explode

Exclude loop* snap devices from lsblk output?

Anatomically Correct Carnivorous Tree



How i can go through database tables inside my django template using “foreign key”


Django foreign key access in save() functiondjango - inlineformset_factory with more than one ForeignKeySaving form data rewrites the same rowShow information of subclass in list_display djangoWhat is wrong with my models.py?Radio buttons in django adminsave() prohibited to prevent data loss due to unsaved related object 'postid'How to expose some specific fields of model_b based on a field of model_a?How to define Mode with generic ForeignKey in DjangoHow to implement update_or_create inside create method of ModelSerializer






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I'm trying to access my database-table inside my django template, but i don't no know the correct syntax to switch from table to another using the foreign key.
Note that my view contain one model, and i am trying to access the others using the foreign key.



I've tried using the "." to switch from table to another but this does not work.



So how i can access Locations,Municipality,Governorate, and Districts tables, from accidents? (In Template)



In models.py:




class Accidents(models.Model):
id_acc = models.CharField(db_column='ID_Acc', primary_key=True, max_length=64) # Field name made lowercase.
id_loc = models.ForeignKey('Locations', db_column='ID_Loc') # Field name made lowercase.
date_acc = models.DateTimeField(db_column='Date_Acc') # Field name made lowercase.


simple_history = HistoricalRecords()

def __unicode__(self):
return '%s' % (self.id_acc)

class Meta:
db_table = 'accidents'


class District(models.Model):
id_d = models.IntegerField(db_column='ID_D', primary_key=True) # Field name made lowercase.
id_gov = models.ForeignKey('Governorate', db_column='ID_Gov') # Field name made lowercase.
name_d = models.TextField(db_column='Name_D') # Field name made lowercase.

class Meta:
db_table = 'district'

def __str__(self):
return self.name_d


class Governorate(models.Model):
id_gov = models.IntegerField(db_column='ID_Gov', primary_key=True) # Field name made lowercase.
name_gov = models.TextField(db_column='Name_Gov') # Field name made lowercase.

class Meta:
db_table = 'governorate'

def __str__(self):
return self.name_gov


class Locations(models.Model):
id_loc = models.AutoField(db_column='ID_Loc', primary_key=True) # Field name made lowercase.
id_mun = models.ForeignKey('Municipality', db_column='ID_Mun' ,default='null') # Field name made lowercase.
lat_loc = models.FloatField(db_column='Lat_Loc') # Field name made lowercase.
lon_loc = models.FloatField(db_column='Lon_Loc') # Field name made lowercase.
name_loc = models.TextField(db_column='Name_Loc') # Field name made lowercase.

class Meta:
db_table = 'locations'

def __str__(self):
return self.name_loc

def __unicode__(self):
return '%s' % (self.name_loc)


class Municipality(models.Model):
id_m = models.IntegerField(db_column='ID_M', primary_key=True) # Field name made lowercase.
id_d = models.ForeignKey(District, db_column='ID_D', blank=True, null=True) # Field name made lowercase.
name_m = models.CharField(db_column='Name_M', max_length=75, blank=True, null=True) # Field name made lowercase.

objects = models.GeoManager()

class Meta:
db_table = 'municipality'


#def __str__(self):
# return self.id_m

def __unicode__(self):
return '%s' % (self.name_m)




In views.py:



class MuniAccidentListView(ListView):
model= Accidents
context_object_name = 'accidents'
template_name = '...../plot_muni.html'



In Template:



var stri = "% for event in accidents %event.date_acc event.id_loc.lat_loc event.id_loc.lon_loc % endfor %";

var strin = "% for event in accidents %event.date_acc event.id_loc.name_loc % endfor %";

var dis = "% for event in accidents %event.id_loc.id_mun.id_d.name_d % endfor %";










share|improve this question






























    0















    I'm trying to access my database-table inside my django template, but i don't no know the correct syntax to switch from table to another using the foreign key.
    Note that my view contain one model, and i am trying to access the others using the foreign key.



    I've tried using the "." to switch from table to another but this does not work.



    So how i can access Locations,Municipality,Governorate, and Districts tables, from accidents? (In Template)



    In models.py:




    class Accidents(models.Model):
    id_acc = models.CharField(db_column='ID_Acc', primary_key=True, max_length=64) # Field name made lowercase.
    id_loc = models.ForeignKey('Locations', db_column='ID_Loc') # Field name made lowercase.
    date_acc = models.DateTimeField(db_column='Date_Acc') # Field name made lowercase.


    simple_history = HistoricalRecords()

    def __unicode__(self):
    return '%s' % (self.id_acc)

    class Meta:
    db_table = 'accidents'


    class District(models.Model):
    id_d = models.IntegerField(db_column='ID_D', primary_key=True) # Field name made lowercase.
    id_gov = models.ForeignKey('Governorate', db_column='ID_Gov') # Field name made lowercase.
    name_d = models.TextField(db_column='Name_D') # Field name made lowercase.

    class Meta:
    db_table = 'district'

    def __str__(self):
    return self.name_d


    class Governorate(models.Model):
    id_gov = models.IntegerField(db_column='ID_Gov', primary_key=True) # Field name made lowercase.
    name_gov = models.TextField(db_column='Name_Gov') # Field name made lowercase.

    class Meta:
    db_table = 'governorate'

    def __str__(self):
    return self.name_gov


    class Locations(models.Model):
    id_loc = models.AutoField(db_column='ID_Loc', primary_key=True) # Field name made lowercase.
    id_mun = models.ForeignKey('Municipality', db_column='ID_Mun' ,default='null') # Field name made lowercase.
    lat_loc = models.FloatField(db_column='Lat_Loc') # Field name made lowercase.
    lon_loc = models.FloatField(db_column='Lon_Loc') # Field name made lowercase.
    name_loc = models.TextField(db_column='Name_Loc') # Field name made lowercase.

    class Meta:
    db_table = 'locations'

    def __str__(self):
    return self.name_loc

    def __unicode__(self):
    return '%s' % (self.name_loc)


    class Municipality(models.Model):
    id_m = models.IntegerField(db_column='ID_M', primary_key=True) # Field name made lowercase.
    id_d = models.ForeignKey(District, db_column='ID_D', blank=True, null=True) # Field name made lowercase.
    name_m = models.CharField(db_column='Name_M', max_length=75, blank=True, null=True) # Field name made lowercase.

    objects = models.GeoManager()

    class Meta:
    db_table = 'municipality'


    #def __str__(self):
    # return self.id_m

    def __unicode__(self):
    return '%s' % (self.name_m)




    In views.py:



    class MuniAccidentListView(ListView):
    model= Accidents
    context_object_name = 'accidents'
    template_name = '...../plot_muni.html'



    In Template:



    var stri = "% for event in accidents %event.date_acc event.id_loc.lat_loc event.id_loc.lon_loc % endfor %";

    var strin = "% for event in accidents %event.date_acc event.id_loc.name_loc % endfor %";

    var dis = "% for event in accidents %event.id_loc.id_mun.id_d.name_d % endfor %";










    share|improve this question


























      0












      0








      0








      I'm trying to access my database-table inside my django template, but i don't no know the correct syntax to switch from table to another using the foreign key.
      Note that my view contain one model, and i am trying to access the others using the foreign key.



      I've tried using the "." to switch from table to another but this does not work.



      So how i can access Locations,Municipality,Governorate, and Districts tables, from accidents? (In Template)



      In models.py:




      class Accidents(models.Model):
      id_acc = models.CharField(db_column='ID_Acc', primary_key=True, max_length=64) # Field name made lowercase.
      id_loc = models.ForeignKey('Locations', db_column='ID_Loc') # Field name made lowercase.
      date_acc = models.DateTimeField(db_column='Date_Acc') # Field name made lowercase.


      simple_history = HistoricalRecords()

      def __unicode__(self):
      return '%s' % (self.id_acc)

      class Meta:
      db_table = 'accidents'


      class District(models.Model):
      id_d = models.IntegerField(db_column='ID_D', primary_key=True) # Field name made lowercase.
      id_gov = models.ForeignKey('Governorate', db_column='ID_Gov') # Field name made lowercase.
      name_d = models.TextField(db_column='Name_D') # Field name made lowercase.

      class Meta:
      db_table = 'district'

      def __str__(self):
      return self.name_d


      class Governorate(models.Model):
      id_gov = models.IntegerField(db_column='ID_Gov', primary_key=True) # Field name made lowercase.
      name_gov = models.TextField(db_column='Name_Gov') # Field name made lowercase.

      class Meta:
      db_table = 'governorate'

      def __str__(self):
      return self.name_gov


      class Locations(models.Model):
      id_loc = models.AutoField(db_column='ID_Loc', primary_key=True) # Field name made lowercase.
      id_mun = models.ForeignKey('Municipality', db_column='ID_Mun' ,default='null') # Field name made lowercase.
      lat_loc = models.FloatField(db_column='Lat_Loc') # Field name made lowercase.
      lon_loc = models.FloatField(db_column='Lon_Loc') # Field name made lowercase.
      name_loc = models.TextField(db_column='Name_Loc') # Field name made lowercase.

      class Meta:
      db_table = 'locations'

      def __str__(self):
      return self.name_loc

      def __unicode__(self):
      return '%s' % (self.name_loc)


      class Municipality(models.Model):
      id_m = models.IntegerField(db_column='ID_M', primary_key=True) # Field name made lowercase.
      id_d = models.ForeignKey(District, db_column='ID_D', blank=True, null=True) # Field name made lowercase.
      name_m = models.CharField(db_column='Name_M', max_length=75, blank=True, null=True) # Field name made lowercase.

      objects = models.GeoManager()

      class Meta:
      db_table = 'municipality'


      #def __str__(self):
      # return self.id_m

      def __unicode__(self):
      return '%s' % (self.name_m)




      In views.py:



      class MuniAccidentListView(ListView):
      model= Accidents
      context_object_name = 'accidents'
      template_name = '...../plot_muni.html'



      In Template:



      var stri = "% for event in accidents %event.date_acc event.id_loc.lat_loc event.id_loc.lon_loc % endfor %";

      var strin = "% for event in accidents %event.date_acc event.id_loc.name_loc % endfor %";

      var dis = "% for event in accidents %event.id_loc.id_mun.id_d.name_d % endfor %";










      share|improve this question
















      I'm trying to access my database-table inside my django template, but i don't no know the correct syntax to switch from table to another using the foreign key.
      Note that my view contain one model, and i am trying to access the others using the foreign key.



      I've tried using the "." to switch from table to another but this does not work.



      So how i can access Locations,Municipality,Governorate, and Districts tables, from accidents? (In Template)



      In models.py:




      class Accidents(models.Model):
      id_acc = models.CharField(db_column='ID_Acc', primary_key=True, max_length=64) # Field name made lowercase.
      id_loc = models.ForeignKey('Locations', db_column='ID_Loc') # Field name made lowercase.
      date_acc = models.DateTimeField(db_column='Date_Acc') # Field name made lowercase.


      simple_history = HistoricalRecords()

      def __unicode__(self):
      return '%s' % (self.id_acc)

      class Meta:
      db_table = 'accidents'


      class District(models.Model):
      id_d = models.IntegerField(db_column='ID_D', primary_key=True) # Field name made lowercase.
      id_gov = models.ForeignKey('Governorate', db_column='ID_Gov') # Field name made lowercase.
      name_d = models.TextField(db_column='Name_D') # Field name made lowercase.

      class Meta:
      db_table = 'district'

      def __str__(self):
      return self.name_d


      class Governorate(models.Model):
      id_gov = models.IntegerField(db_column='ID_Gov', primary_key=True) # Field name made lowercase.
      name_gov = models.TextField(db_column='Name_Gov') # Field name made lowercase.

      class Meta:
      db_table = 'governorate'

      def __str__(self):
      return self.name_gov


      class Locations(models.Model):
      id_loc = models.AutoField(db_column='ID_Loc', primary_key=True) # Field name made lowercase.
      id_mun = models.ForeignKey('Municipality', db_column='ID_Mun' ,default='null') # Field name made lowercase.
      lat_loc = models.FloatField(db_column='Lat_Loc') # Field name made lowercase.
      lon_loc = models.FloatField(db_column='Lon_Loc') # Field name made lowercase.
      name_loc = models.TextField(db_column='Name_Loc') # Field name made lowercase.

      class Meta:
      db_table = 'locations'

      def __str__(self):
      return self.name_loc

      def __unicode__(self):
      return '%s' % (self.name_loc)


      class Municipality(models.Model):
      id_m = models.IntegerField(db_column='ID_M', primary_key=True) # Field name made lowercase.
      id_d = models.ForeignKey(District, db_column='ID_D', blank=True, null=True) # Field name made lowercase.
      name_m = models.CharField(db_column='Name_M', max_length=75, blank=True, null=True) # Field name made lowercase.

      objects = models.GeoManager()

      class Meta:
      db_table = 'municipality'


      #def __str__(self):
      # return self.id_m

      def __unicode__(self):
      return '%s' % (self.name_m)




      In views.py:



      class MuniAccidentListView(ListView):
      model= Accidents
      context_object_name = 'accidents'
      template_name = '...../plot_muni.html'



      In Template:



      var stri = "% for event in accidents %event.date_acc event.id_loc.lat_loc event.id_loc.lon_loc % endfor %";

      var strin = "% for event in accidents %event.date_acc event.id_loc.name_loc % endfor %";

      var dis = "% for event in accidents %event.id_loc.id_mun.id_d.name_d % endfor %";







      django django-models django-rest-framework django-templates






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 25 at 22:35







      M.Dimassi

















      asked Mar 23 at 11:00









      M.DimassiM.Dimassi

      12




      12






















          0






          active

          oldest

          votes












          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55312995%2fhow-i-can-go-through-database-tables-inside-my-django-template-using-foreign-ke%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55312995%2fhow-i-can-go-through-database-tables-inside-my-django-template-using-foreign-ke%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          1973년 목차 사건 문화 탄생 사망 노벨상 달력 둘러보기 메뉴

          SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

          인천여자상업고등학교 목차 학교 연혁 설치 학과 학교 동문 참고 자료 각주 외부 링크 둘러보기 메뉴북위 37° 28′ 05″ 동경 126° 37′ 41″ / 북위 37.4680025° 동경 126.6279602°  / 37.4680025; 126.6279602인천여자상업고등학교“인천광역시립학교 설치조례 별표1”인천여자상업고등학교 홈페이지eheh문서를 완성해