Django - Polymorphic Models or one big Model?

此生再无相见时 提交于 2019-12-21 07:26:28

问题


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?


回答1:


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.




回答2:


We ended up going with a large table with a lot of usually-empty columns. Our reasoning was that (in this case) our Mammal table was all we'd be querying over, and there was no (intuitive) way to filter out by certain types of Mammals besides manually checking whether they had a "dolphin" or "elephant" object, which then threw an error if they didn't. Even looking for the type of an object returned from a query that was definitely an Elephant still returned "Mammal". It would be hard to extend any Pythonic workarounds to writing pure SQL, which one of our data guys does regularly.



来源:https://stackoverflow.com/questions/16310930/django-polymorphic-models-or-one-big-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!