简体   繁体   English

Django:相同的 model 具有不同的功能?

[英]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.然后,您可以让您的RestaurantHotel模型继承此抽象 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表包含所有类别。
  • now there may have some common and unique feature for every category.现在每个类别可能都有一些共同和独特的功能。 features will have many to many relation with category table. features将与category表有many to many关系。
    so we create 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.

相关问题 Django为同一模型创建不同的表 - Django creating different tables for same model Django 中的同一 model 中具有更多字段的不同用户 - Different user with more fields in the same model in Django django UserProfile和RegistrationProfile:相同或不同的模型 - django UserProfile and RegistrationProfile : Same model or different Django 中相同 model 的 UpdateView 的不同模板 - Different templates for UpdateView for the same model in Django 我有一个在python中训练过的XGBoost模型,但是当它加载到scala并使用相同的功能时会得到不同的预测,为什么? - I have a XGBoost model trained in python, but it will get a different predictions when loaded in scala and used the same features, why? 如何在Django中保存具有相同模型但具有不同数据的数据 - How to save the data that has same model but with different data in Django Django:在相同模型但不同类中添加外键 - Django: Add foreign key in same model but different class 将django模型连接到具有相同结构的不同表名 - Connect django model to different table name with same structure 在Django中使用具有相同值的不同模型字段 - Use different model's field with the same value in Django 对流水线中的同一个估计器使用不同的特征 - Using different features for the same estimator in the pipeline
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM