[英]Django: Same model with different features?
Object Model: Object Model:
class Object(models.Model):
author = models.ForeignKey(ProfileUser, on_delete=models.CASCADE)
title = models.CharField(max_length=300)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
address = models.CharField(max_length=300)
content = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
approved_object = models.BooleanField(default=False)
admin_seen = models.BooleanField(default=False)
def __str__(self):
return f"{self.title}"
Category model:类别 model:
class Category(models.Model):
title = models.CharField(max_length=50)
def __str__(self):
return f"{self.title}"
For example I have some categories, like hotels, restaurants etc. So I want for each category to have different features (radio buttons when adding new), but I'm not sure, how to handle it properly.例如,我有一些类别,如酒店、餐馆等。所以我希望每个类别都有不同的功能(添加新的单选按钮),但我不确定如何正确处理它。 Hotels must have rooms, pool etc. Restaurants must have country kitchen, seats etc. In future I will have and other categories.酒店必须有房间,游泳池等。餐馆必须有乡村厨房,座位等。将来我会有其他类别。
Quesion is: Which is the best way (practice) to do this.问题是:这是最好的方法(实践)。
My solution: To create third table with features and every category to have column features
and store features separate by comma, but it's not very good solution based on DB normalization .我的解决方案:创建具有特征的第三个表,并且每个类别都有列features
并用逗号分隔存储特征,但这不是基于DB normalization的很好的解决方案。
You could use abstract base classes :您可以使用抽象基类:
Abstract base classes are useful when you want to put some common information into a number of other models.当您想将一些公共信息放入许多其他模型中时,抽象基类很有用。 You write your base class and put abstract=True in the Meta class.您编写基础 class 并将 abstract=True 放入 Meta class 中。 This model will then not be used to create any database table.然后,此 model 将不会用于创建任何数据库表。 Instead, when it is used as a base class for other models, its fields will be added to those of the child class.相反,当它用作其他模型的基础 class 时,其字段将添加到子 class 的字段中。
You can then make your Restaurant
and Hotel
models inherit from this abstract class.然后,您可以让您的Restaurant
和Hotel
模型继承此抽象 class。 Django will then create two tables with the base fields from the abstract class plus the specific fields from each models.然后 Django 将创建两个表,其中包含来自抽象 class 的基本字段以及来自每个模型的特定字段。
You can use multi-table inheritance可以使用多表 inheritance
You define a base object, and from there you can define different child objects which will share the parent's properties您定义一个基础 object,然后您可以从那里定义共享父属性的不同子对象
In your case that would look something like this:在你的情况下,看起来像这样:
class Object(models.Model):
...
class Restaurant(Object):
seats = IntegerField(...)
reservations = ManyToManyField(...)
class Hotel(Object):
rooms = IntegerField(...)
has_pool = BooleanField(...)
Django will automatically create relationships and manage the querying for you. Django 将自动为您创建关系并管理查询。 To get all restaurants, you can use Restaurant.objects.all()
, there is a limitation though.要获取所有餐厅,您可以使用Restaurant.objects.all()
,但有一个限制。 When querying Object.objects.all()
, you will get a list of the Objects
, not their specific subclass.查询Object.objects.all()
时,您将获得Objects
的列表,而不是它们的特定子类。 If I remember correctly, you can access the specific instance through (for instance) object.restaurant
.如果我没记错的话,您可以通过(例如) object.restaurant
访问特定实例。
If you do want to get the specific objects, you can look into a library called Django-polymorphic .如果您确实想获取特定对象,可以查看一个名为Django-polymorphic的库。
i will explain this one by one:我将一一解释:
category
table contains all categories. category
表包含所有类别。features
will have many to many
relation with category
table. features
将与category
表有many to many
关系。feature_master
table and we will map it with category table.所以我们创建feature_master
表,我们将 map 它与类别表。feature_master
table contains all features. feature_master
表包含所有功能。category_feature_map
table is map table(junction table) category_feature_map
表是 map 表(连接表)object
table have all the detail about object and object_detail
table will contain all the feature
to particular object object
表包含有关 object 的所有详细信息,而object_detail
表将包含特定 object 的所有feature
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.