簡體   English   中英

Django Models.py數據庫設計反饋

[英]Django Models.py Database Design Feedback

根據我之前的問題和反饋,我重新設計了模型,並在運行“ syncdb ”之前需要一些反饋。

我主要關心的是ForeignKeys和Restaurant表中的一個ManyToManyField ManyTomany字段也應該具有through =''值,該值應該是什么?

任何反饋表示贊賞!

楷模

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

考慮到coisine_types的概念和含義,您不必使用through關鍵字來建立關系。 當存在有關其自身關系的某些信息時,您將使用它(主要是)。

根據Django文檔:

此選項最常見的用法是當您要將多余的數據與多對多關系關聯時。

請參閱此處的說明: 多對多關系的額外字段

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM