Django - 网上商店模型组织 [英] Django - Web Store Model Organization

查看:115
本文介绍了Django - 网上商店模型组织的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始在Django创建一个网络商店。它会有不同种类的产品,如衬衫,海报,杯子,贴纸。每种类型的产品都会有不同的领域。例如:衬衫将具有尺寸和颜色选项,贴纸将具有尺寸选项,海报和杯子将没有选项。



最好的方法是什么?遗产?关系?



我还在学习Django,想了解更多有经验的人,然后深入了解这种项目。



谢谢

解决方案

这是一个有趣的问题。我相信首先你必须弄清楚,如果你的用户想要自己添加项目类型,或者如果有一个预定义的类型,只有开发人员可以添加一个新的,如果需要的话。



静态项目类型



如果项目类型是静态的,您可以使用模型继承来创建模式。因此,在这种情况下,我建议为每个项目类型定义一个或多个抽象基础模型和具体模型。例如,你可以这样做:

 
类项目(models.Model):
slug = models.SlugField()
price = models.DecimalField()
remaining = models.IntegerField()
description = models.CharField()

class Meta:
abstract = True

class ClothingItem(Item):
size = models.CharField()
brand = models.CharField()

class Meta:
abstract = True

class Jacket(ClothingItem):
has_hood = models.BooleanField()

class Hat(ClothingItem):
hat_type = models.CharField(choices = [])

所以,上面实际上只会生成两个数据库表:夹克和帽子并且每个将包含它从其继承的模型中的所有字段。这是大多数人在django中使用的,因为它非常干净,使得添加表单和生成查询非常容易获取项目。



您的其他选项是不使用抽象基类,因此您将获得名为 Item 的数据库表,包含每个字段,另一个名为 ClothingItem 的表将包含一个ForeignKey到项目,另一个名为夹克将包含一个ForeignKey到 ClothingItem 。这将帮助您在项目 ClothingItem 上生成聚合,但是django将不得不查询三个表(使用连接)每当你想获得一个夹克的属性。



动态项目类型
在这种情况下,您必须使用不同的模式,因为Items的属性必须由应用程序的用户定义。你可以这样做:

 
class类别(models.Model):
name = models.CharField()

class Attribute(models.Model):
category = models.ForeignKey(Category)
name = models.CharField

class Item(models.Model) :
slug = models.SlugField()
price = models.DecimalField()
remaining = models.IntegerField()
category = models.ForeignKey(Category)
description = models.CharField()
attributes = models.ManyToManyField(Attribute,through ='ItemAttribute')

class ItemAttribute(models.Model):
item = models.ForeignKey (Item)
attribute = models.ForeignKey(Attribute)
value = models.CharField()

有点复杂一些。我们在这里是一个类别模型,将定义您的项目类型(帽子,夹克,杯子,衬衫,海报等) - 您还可以定义一个类别的层次结构,我将离开作为一个练习。属性模型定义属性的名称 - 每个属性都有一个名称和属于它的类别。因此,对于夹克类别,您将拥有一个has_hood属性,对于帽子类别为hat_type属性等。



现在,一个Item模型也属于一个类别,并且具有通过ItemAttribute模型与属性模型的多对多关系。最后一个意味着您将在数据库中具有
ItemAttribute表,其中包含以下字段:

 
项目 - 属性值因此,夹克312将具有值为true的属性2(has_hood),而夹套313将具有属性2,其中,值为false等。



上面的设计有一个小问题,属性中没有类型。您应该通过向Attribute模型添加_type属性来扩展它。另外,请检查这个问题的答案:
Django动态模型字段< a>以更通用的方式定义动态模型字段



通过此设计,您的用户将能够创建新属性,将其分配给类别,所以当他们添加新的项目时,他们将能够根据他们的类别来填充他们的属性。当然,为了使用户能够执行此操作,您还必须生成动态表单,其中将包含每个项目的动态属性。如果你想我也可以告诉你如何做到这一点 - 它并不像人们首先相信那么困难(提示:使用类型来生成动态Form类)。


I am starting to create a web store in Django. It will have different kinds of products, like shirts, posters, mugs, stickers. Each type of product will have different fields. For ex: shirts will have size and color options, stickers will have size option, posters and mugs will have no options.

What is the best way to do this? Inheritance? Relationships?

I'm still learning Django and want to know about that from more experienced people before i dig deeper on this kind of project.

Thanks

解决方案

This is an interesting problem. I believe that first of all you must find out if your users would like to add item types themselves or if there would be a predefined number of types and only a developer could add a new one if needed.

Static Item Types

If the item types are static you can use model inheritance to create your schema. So, in this case I recommend defining one or more abstract base models and concrete models for each of your item types. For instance you would do something like this:

class Item(models.Model):
  slug = models.SlugField()
  price = models.DecimalField()
  remaining = models.IntegerField()
  description = models.CharField()

  class Meta:
    abstract=True

class ClothingItem(Item):
  size = models.CharField()
  brand = models.CharField()

  class Meta:
    abstract=True

class Jacket(ClothingItem):
  has_hood = models.BooleanField()

class Hat(ClothingItem):
  hat_type = models.CharField(choices=[])  

So, the above will actually generate only two database tables: Jacket and Hat and each one will contain all the fields from the models it has inherited from. This is what most people use in django because it is very clean makes it very easy to add forms and generate queries to get Items.

Your other options is to not use abstract base classes, so you will get a database table named Item which will contain each fields, another table named ClothingItem that will contain a ForeignKey to the Item and another named Jacket that will contain a ForeignKey to the ClothingItem. This will help you in generating aggregations on Item or ClothingItem, however django will have to query three tables (with joins) whenever you want to get the properties of a Jacket. I recommend using this only if you are positive that you need it.

Dynamic Item Types In this case you have to use a different schema because the properties of the Items have to be defined by the user of the application. You may do something like this:

class Category(models.Model):
  name = models.CharField()

class Attribute(models.Model):
  category = models.ForeignKey(Category)
  name = models.CharField

class Item(models.Model):
  slug = models.SlugField()
  price = models.DecimalField()
  remaining = models.IntegerField()
  category = models.ForeignKey(Category)
  description = models.CharField()
  attributes = models.ManyToManyField(Attribute, through='ItemAttribute')

class ItemAttribute(models.Model):
  item= models.ForeignKey(Item)
  attribute = models.ForeignKey(Attribute)
  value = models.CharField()

This design is a little more complex. What we have here is a category model that will define your item type (hat, jacket, mug, shirt, poster etc) -- you could also define a hierarchy of categories which I will leave as an exercise to you. The Attribute model defines the names of attributes - each one has a name and a category it belongs to. So for the jacket category you will have a has_hood attribute, for the hat category a hat_type attribute etc.

Now, an Item model belongs also to a Category and has a many-to-many relation with the Attribute model through the ItemAttribute model. The last one means the you will have an ItemAttribute table in your database with the following fields:

item - attribute - value

So the jacket 312 will have the attribute 2(has_hood) with a value of true while the jacket 313 will have the attribute 2 withe a value of false etc.

The above design has the minor problem that there is no "type" in the attributes. You should extend it more by adding a _type attribute to the Attribute model. Also, please check the answers to this question: Django dynamic model fields which defines dynamic model fields in a more generic way.

With this design your users will be able to create new attributes, assign them to categories, so when they will add new item they would be able to fill their attributes based on their category. Of course, to enable your users to do this you also have to generate dynamic forms that will contain your dynamic attributes for each item. If you want I can tell you also how to do that - it's not as difficult as somebody believes at first (hint: use type to generate dynamic Form classes).

这篇关于Django - 网上商店模型组织的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆