Django Models.py数据库设计反馈

时间:2014-03-12 19:39:46

标签: python mysql database django

根据我的previous question和我收到的反馈,我在运行" syncdb "之前重新设计了模型并需要一些反馈。

我的顾虑主要是 ForeignKeys 和餐厅表中的 ManyToManyField 。 ManyTomany字段是否还应具有到='' 值以及该值应该是什么?

感谢任何反馈!

模型

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'
....

1 个答案:

答案 0 :(得分:0)

考虑到coisine_types的概念和含义,您不必使用through关键字建立关系。当有关于它自身关系的一些信息时,你(大部分)都会使用它。

根据Django文档:

  

此选项最常见的用途是,您希望将额外数据与多对多关系相关联。

请参阅此处的说明:Extra fields on many-to-many relationships