Django - 多形态模型或一个大模型? [英] Django - Polymorphic Models or one big Model?

查看:99
本文介绍了Django - 多形态模型或一个大模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在Django开发一个模型,涉及一种可以具有各种不同特征的模型,具体取决于它是什么样的对象。所以,假设我们有一个叫做哺乳动物的模型,它可以是大象还是海豚(分别具有tusk_length和flipper_length)。



基本的OOP原则喊多态,我倾向于同意。但是,由于我刚刚开始Django,我首先想知道Django是否是最好的方法。我听说过很多例子,有些人喜欢单身巨型模特们。



我已经尝试过使用GenericForeignKeys,如下所述:如何将Django的GenericForeignKey限制为模型列表?。虽然这个解决方案工作很好,我不喜欢无法过滤,而且这种关系只是一种方式。也就是说,虽然您可以从哺乳动物对象获取海豚,但您无法从Dolphin获取哺乳动物对象。



所以,这是我的两个选择:



选择A:

 从django.db导入模型
class Mammal(models.Model):
hair_length = models.IntegerField()
tusk_length = models.IntegerField()
flipper_length = models.IntegerField()
animal_type = models .CharField(max_length = 15,choices = [Elephant,Dolphin]

选择B:来自django.db的



$ b $哺乳动物(models.Model):
hair_length =模型。 IntegerField()

class Elephant(Mammal):
tusk_length = models.IntegerField()

class Dolphin(Mammal):
flipper_length = models。 IntegerField()

选择B,从我所了解的,在查询和列出时具有更好的代码的优势所有大象或海豚。然而,我注意到,从哺乳动物列表中获取所有的Elephants(在这里有一个查询)并不直接,而不会在动画类中放置animal_type,默认值依赖于该类。



这将导致另一个我看到的多态性的问题,这在上面的例子或我的应用中不会出现,但值得一提的是,这将很难将Dolphin对象编辑成大象,而不是完全删除Dolphin。



总的来说,有没有一般偏好,还是有什么大的原因我不应该使用多态? >

解决方案

我的建议,一般来说,数据库设计是避免继承。这使得访问和更新都变得复杂。



在Django中,尝试使用抽象类作为基本模型。这意味着不会为其创建数据库表。其字段/列将在其子模型中自动创建。其优点是:Django / Python代码中的代码重用以及数据库中简单,平面的设计。惩罚是:管理/查询小孩模型的混合集更多的工作。



在这里看到一个例子: Django模式:模型继承



或者,您可以更改哺乳动物到哺乳动物之旅。并在每个特定的哺乳类中包含一个MammalTraits对象。在代码中,即组合(has-a)。在db中,这将被表示为外键。


I'm currently working on a model in Django involving one model that can have a variety of different traits depending on what kind of object it is. So, let's say we have a model called Mammal, which can either be an Elephant or a Dolphin (with their own traits "tusk_length" and "flipper_length" respectively).

Basic OOP principles shout "polymorphism", and I'm inclined to agree. But, as I'm new to Django, I first want to know whether or not it is the best way to do so in Django. I've heard of plenty of examples of and some people giving their preferences toward singular giant models

I've already tried using GenericForeignKeys as described here: How can I restrict Django's GenericForeignKey to a list of models?. While this solution works beautifully, I don't like the inability to filter, and that the relationship is only one way. That is, while you can get a Dolphin from a Mammal object, you can't get the Mammal object from the Dolphin.

And so, here are my two choices:

Choice A:

from django.db import models
class Mammal(models.Model):
    hair_length = models.IntegerField()
    tusk_length = models.IntegerField()
    flipper_length = models.IntegerField()
    animal_type = models.CharField(max_length = 15, choices = ["Elephant", "Dolphin"]

Choice B:

from django.db import models
class Mammal(models.Model):
    hair_length = models.IntegerField()

class Elephant(Mammal):
    tusk_length = models.IntegerField()

class Dolphin(Mammal):
    flipper_length = models.IntegerField()

Choice B, from what I understand, has the advantage of nicer code when querying and listing all Elephants or Dolphins. However, I've noticed it's not as straightforward to get all of the Elephants from a list of Mammals (is there a query for this?) without putting animal_type in the class, with default being dependent on the class.

This leads to another problem I see with polymorphism, which won't come up in this example above or my application, but is worth mentioning is that it would be difficult to edit a Dolphin object into an Elephant without deleting the Dolphin entirely.

Overall, is there any general preference, or any big reason I shouldn't use polymorphism?

解决方案

My recommendation, in general with database design, is to avoid inheritance. It complicates both the access and updates.

In Django, try using an abstract class for your base model. That means a db table will not be created for it. Its fields/columns will be auto-created in its child models. The benefit is: code reuse in Django/Python code and a simple, flat design in the database. The penalty is: it's more work to manage/query a mixed collection of child models.

See an example here: Django Patterns: Model Inheritance

Alternatively, you could change the concept of "Mammal" to "MammalTraits." And include a MammalTraits object inside each specific mammal class. In code, that is composition (has-a). In the db, that will be expressed as a foreign key.

这篇关于Django - 多形态模型或一个大模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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