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

          How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

          155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

          Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?