[英]Django Models.py Database Design Feedback
Based on my previous question and feedback I received I have redesigned my Models and need some feedback before I run the " syncdb ". 根据我之前的问题和反馈,我重新设计了模型,并在运行“ syncdb ”之前需要一些反馈。
My concerns are mostly ForeignKeys and the one ManyToManyField in the Restaurant table. 我主要关心的是ForeignKeys和Restaurant表中的一个ManyToManyField 。 Should also the ManyTomany field have the through='' value and what the value should be?
ManyTomany字段也应该具有through =''值,该值应该是什么?
Any feedback is appreciated! 任何反馈表示赞赏!
Models 楷模
class Restaurant(models.Model):
id = models.AutoField(primary_key=True, db_column='id')
name = models.CharField(max_length=50L, db_column='name', blank=True)
address = models.CharField(max_length=100L, blank=True)
city_id = models.ForeignKey('City', related_name="restaurant_city")
location_id = models.ForeignKey('Location', related_name="restaurant_location")
hood_id = models.ForeignKey('Hood', null=True, blank=True, related_name="restaurant_hood")
listingrole_id = models.ForeignKey('Listingrole', related_name="restaurant_listingrole")
cuisine_types = models.ManyToManyField('Cuisinetype', null=True, blank=True, related_name="restaurant_cuisinetype")
class Meta:
db_table = 'restaurant'
class City(models.Model):
id = models.AutoField(primary_key=True, db_column='id')
name = models.CharField(max_length=50L, db_column='city')
state = models.CharField(max_length=50L, db_column='state', blank=True, null=True)
class Meta:
db_table = 'city'
class Cuisinetype(models.Model):
id = models.AutoField(primary_key=True, db_column='id')
name = models.CharField(max_length=50L, db_column='cuisine', blank=True) # Field name made lowercase.
class Meta:
db_table = 'cuisinetype'
class Location(models.Model):
id = models.AutoField(primary_key=True, db_column='id')
name = models.CharField(max_length=50L, db_column='location', blank=False, null=False)
city = models.ForeignKey('City', related_name="location_city")
class Meta:
db_table = 'location'
class Hood(models.Model):
id = models.AutoField(primary_key=True, db_column='id')
name = models.CharField(max_length=50L, db_column='hood')
city = models.ForeignKey('City', related_name='hood_city')
location = models.ForeignKey('Location', related_name='hood_location')
class Meta:
db_table = 'hood'
class Listingrole(models.Model):
id = models.AutoField(primary_key=True, db_column='id')
name = models.CharField(max_length=50L, db_column='listingrole', blank=True) # Field name made lowercase.
class Meta:
db_table = 'listingrole'
....
Having into account the concept and meaning of coisine_types
you don't have to make the relationship using through
keyword. 考虑到
coisine_types
的概念和含义,您不必使用through
关键字来建立关系。 You use it (mostly) when there is some information about the relation it self. 当存在有关其自身关系的某些信息时,您将使用它(主要是)。
According Django documentation: 根据Django文档:
The most common use for this option is when you want to associate extra data with a many-to-many relationship.
此选项最常见的用法是当您要将多余的数据与多对多关系关联时。
See explanation here: Extra fields on many-to-many relationships 请参阅此处的说明: 多对多关系的额外字段
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.