gpt4 book ai didi

python - Django Models.py 数据库设计反馈

转载 作者:行者123 更新时间:2023-11-30 23:04:46 25 4
gpt4 key购买 nike

基于我的 previous question和我收到的反馈 我重新设计了我的模型,在运行“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 文档:

The most common use for this option is when you want to associate extra data with a many-to-many relationship.

请参阅此处的解释:Extra fields on many-to-many relationships

关于python - Django Models.py 数据库设计反馈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22362123/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com