Django foreign key reference name conflict -


location app model:

class states(models.model):     state_name = models.charfield(max_length=200, unique=true)     update_time = models.datetimefield(auto_now=true, auto_now_add=false)     create_time = models.datetimefield(auto_now=false, auto_now_add=true)      def __str__(self):          return self.state_name  class districts(models.model):     state = models.foreignkey(states, on_delete=models.cascade)     dis_name = models.charfield(max_length=200, unique=true)     update_time = models.datetimefield(auto_now=true, auto_now_add=false)     create_time = models.datetimefield(auto_now=false, auto_now_add=true)      def __str__(self):          return self.dis_name  class places(models.model):     district    =  models.foreignkey(districts, on_delete=models.cascade)     place_name  =  models.charfield(max_length=200, unique=true,)     pin_no      =  models.integerfield(null=true)     update_time =  models.datetimefield(auto_now=true, auto_now_add=false)     create_time =  models.datetimefield(auto_now=false, auto_now_add=true)      def __str__(self):          return self.place_name 

suppose, want map info district name. in admin panel shows district name. (but conflict 2 states have same district name)

i want use way: working app model:

from location.models import states, districts, places     class info(models.model):            dis_name    =  models.foreignkey(districts, on_delete=models.cascade)         info_name   =  models.charfield(max_length=500)         sort_name   =  models.charfield(max_length=50) 

in admin.py

class infoadmin(admin.modeladmin):     list_display = ("dis_name", "info_name", "sort_name")     ordering = ('id',) 

but in admin panel shows district name.

so should do. avoid conflict. if use own id removes conflict. in admin panel shows id field of district. not human readable.

example:

all want, suppose 2 state have same district name state delhi , kolkata have same district name darjeeling. in admin panel 2 darjeeling shows. how can sure district comes state?

you made dis_name unique in district class. prevent having more 1 districts of same name, if in different states.

same goes place_name in places class.

try with:

class districts(models.model):   state = models.foreignkey(states, on_delete=models.cascade)   dis_name = models.charfield(max_length=200, unique=false)   update_time = models.datetimefield(auto_now=true, auto_now_add=false)   create_time = models.datetimefield(auto_now=false, auto_now_add=true)    def __str__(self):       return self.dis_name +' - '+ self.state.state_name  class places(models.model):   district    =  models.foreignkey(districts, on_delete=models.cascade)   place_name  =  models.charfield(max_length=200, unique=false)   pin_no      =  models.integerfield(null=true)   update_time =  models.datetimefield(auto_now=true, auto_now_add=false)   create_time =  models.datetimefield(auto_now=false, auto_now_add=true)    def __str__(self):      return self.place_name + ' - ' + self.district.dis_name 

update : show corresponding state name district name in admin panel, , district name place name


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -