Python Django模型中的关系问题
我想知道我的模型关系是否正确。我正在创建一个发现平台,允许用户搜索各种产品项目。谢谢你的帮助 我有四种Python Django模型中的关系问题,python,django,django-models,Python,Django,Django Models,我想知道我的模型关系是否正确。我正在创建一个发现平台,允许用户搜索各种产品项目。谢谢你的帮助 我有四种型号:产品、精品店、产品类别和设计师 每个产品都有设计师。 每个产品都有一个产品类别。 每家精品店都有一款产品,该产品与设计师相关 每个设计师都有一个产品,该产品与精品店相关。 Mymodel.py代码如下: 设计师 class Designer(models.Model): name = models.CharField(max_length=254, blank=True, null
型号
:产品、精品店、产品类别和设计师
每个产品都有设计师。
每个产品都有一个产品类别。
每家精品店都有一款产品,该产品与设计师相关
每个设计师都有一个产品,该产品与精品店相关。
Mymodel.py
代码如下:
设计师
class Designer(models.Model):
name = models.CharField(max_length=254, blank=True, null=True)
label_name = models.CharField(max_length=254, blank=True, null=True)
description = models.CharField(max_length=254, null=True, blank=True)
specialites = models.CharField(max_length=254, null=True, blank=True)
image = models.ImageField(upload_to='images/designers/main',max_length=100, null=True) #For the argument upload_to, will add to the static folder and generated image will be stored in suing that path specified
#For Admin Purposes, to track and see which if still active by for administrative users only
is_active = models.BooleanField(default=True)
#Foreign Keys & other relationships
product = models.ManyToManyField(Product)
boutiques = models.ManyToManyField(Boutique)
#Metadata
class Meta:
verbose_name = _("Designer Information")
#Helps return something meaningful, to show within the admin interface for easy interaction
def __unicode__(self):
return self.name
产品
class Product(models.Model):
name = models.CharField(max_length=254, blank=True, null=True)
description = models.CharField(max_length=254, blank=True, null=True)
color_name = models.CharField(max_length=254, null=True, blank=True)
size_types = models.CharField(max_length=7, null=True, blank=True)
product_price = models.DecimalField(max_digits=9,decimal_places=2)
old_price = models.DecimalField(max_digits=9,decimal_places=2, blank=True,default=0.00) #To show original price if, new price has been added
product_tags = models.CharField(max_length=254, null=True, blank=True, help_text='Comma-delimited set of SEO keywords for product tag area')
novelty = models.CharField(max_length=254, null=True, blank=True)
product_website = models.URLField(max_length=200, null=True, blank=True) #To show other sites to Users, where they can purchase the particular product
image = models.ImageField(upload_to='images/products/main',max_length=100, null=True) #For the argument upload_to, will add to the static folder and generated image will be stored in suing that path specified
slug = models.SlugField(max_length=255, unique=True, help_text='Unique value for product page URL, created from name.')
#This shows when each item was uploaded & by who, to the User
uploaded_by = models.CharField(max_length=254, blank=True, null=True)
uploaded_at = models.DateTimeField(auto_now=True)
#For Admin Purposes, to track and see which if still active by for administrative users only
is_active = models.BooleanField(default=True)
#Foreign Keys & other relationships
categories = models.ManyToManyField(ProductCategory)
boutiques = models.ManyToManyField(Boutique)
designer = models.ForeignKey(Designer)
#Metadata
class Meta:
verbose_name = _("Product")
verbose_name_plural = _("Products")
#Helps return something meaningful, to show within the admin interface for easy interaction
def __unicode__(self):
return self.name
产品类别
class ProductCategory(models.Model):
name = models.CharField(max_length=255L, blank=True, null=True)
slug = models.SlugField(max_length=50, unique=True, help_text='Unique value for product page URL, created from name.')
#For Admin Purposes, to track and see which if still active by for administrative users only
is_active = models.BooleanField(default=True)
#For Admin Purposes, to track when we add each product and each product was updated by administrative users
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
#Helps return something meaningful, to show within the admin interface for easy interaction
def __unicode__(self):
return self.name
精品店
class Boutique(models.Model):
boutique_name = models.CharField(max_length=254, blank=True, null=True)
address = models.CharField(max_length=255, blank=True, null=True)
city = models.CharField(max_length=50, null=True, blank=True)
state = models.CharField(max_length=2, null=True, blank=True)
zipcode = models.IntergerField(max_length=5, null=True, blank=True)
boutique_website = models.URLField(max_length=200, null=True, blank=True)
#For Admin Purposes, to track a product to see which is active by administrative users
is_active = models.BooleanField(default=True)
#Foreign Keys & other relationships
product = models.ManyToManyField(Product)
designer = models.ManyToManyField(Designer)
#Metadata
class Meta:
verbose_name = _("Boutique Information")
#Helps return something meaningful, to show within the admin interface for easy interaction
def __unicode__(self):
return self.name
我怀疑你的
型号是否正确
我想这就是你想要做的:
有一个设计器
一个设计师
可以拥有许多精品店
一家精品店
可以有许多产品
产品
也与产品类别
相关李>
因此,这种关系应该类似于设计师>精品店>产品
,即设计师
创建一个精品店
,并在与产品类别
关联后在其中添加一些产品
这意味着:
Designer
不应该有任何ForeignKey
字段,而是它应该是精品店
和产品
模型的ForeignKey
模型精品店
应具有与设计师
相关的外键
,不至产品
李>
型号ProductCategory
不应包含ForeignKey
字段。但是它应该是产品的外键
型号产品
应该有三个外键
与设计师
、精品
和产品类别
相关
因此,通过所有这些,您的模型应该如下所示:
class Designer(models.Model):
name = models.CharField(max_length=100)
# Other fields here ...
# Don't create any ForeignKey fields
class Boutique(models.Model):
designer = models.ForeignKey(Designer)
# Other fields here ...
# No need for any more ForeignKeys
class ProductCategory(models.model):
name = models.CharField(max_length=100)
# Other fields ...
# This model doesn't need any ForeignKey field
class Product(models.Model):
designer = models.ForeignKey(Designer)
boutique = models.ForeignKey(Boutique)
category = models.ForeignKey(ProductCategory)
name = models.CharField(max_length=100)
# Other fields here ...
这与Django文档中的民意测验教程非常相似。在这里,您可以创建一个具有多种选择的投票。因此,Choice
模型有一个ForeignKey
toPoll
模型,反之亦然