Django:在这种情况下使用抽象基础模型是个好主意吗?

发布于 2024-08-19 07:28:00 字数 321 浏览 3 评论 0原文

class Account(models.Model):
        identifier = models.CharField(max_length=5)
        objects = MyCustomManager()

        class Meta:
            abstract = True

class Customer(Account):
        name = models.CharField(max_length=255)

如果我有很多模型,并且我想节省时间,不用到处放置外键,这样对吗?或者,我认为这一切都是错误的吗?

class Account(models.Model):
        identifier = models.CharField(max_length=5)
        objects = MyCustomManager()

        class Meta:
            abstract = True

class Customer(Account):
        name = models.CharField(max_length=255)

If I have a lot of models, and I want to save time from having to put foreignkeys everywhere, is this right? Or, am I thinking of this all wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷︶言冷语的世界 2024-08-26 07:28:00

这取决于外键的方向。您不能拥有抽象类的外键。

也许这是您感兴趣的通用关系或者抽象模型类中的外键< /a>.

尽管请注意,继承始终是一种is-a 关系,而正常的外键使用则意味着一种has-a 关系。

在您的示例中,Customer 不应从 Account 继承,因为客户拥有帐户。

一个继承示例是一个地点,它可以是餐厅电影院等。

评论后编辑:

,在 文档

类继承和模型管理器并不是完美匹配。管理器通常特定于它们定义的类,并且在子类中继承它们不一定是一个好主意。另外,由于声明的第一个管理器是默认管理器,因此允许对其进行控制非常重要。以下是 Django 处理自定义管理器和模型继承的方式:

...

  • 来自抽象基类的管理器始终由子类继承,使用 Python 的正常名称解析顺序(子类上的名称覆盖所有其他名称;然后是第一个父类上的名称,依此类推)。抽象基类旨在捕获其子类共有的信息和行为。定义通用管理器是此通用信息的适当部分。
  • 类的默认管理器是该类上声明的第一个管理器(如果存在),或者是父层次结构中第一个抽象基类的默认管理器(如果存在)。如果没有显式声明默认管理器,则使用 Django 的普通默认管理器。

仅当继承的类在某种程度上属于同一范围时我才会这样做。
如果您确实拥有如此多的类,以至于向这些类添加一行很重要,那么您可能没有良好的数据库或应用程序设计。

并且尽量不要将所有内容都放在一个管理器中,以便能够在很多类中只使用一个管理器。

It depends in which direction the foreign keys go. You cannot have a foreign key to an abstract class.

Maybe it is Generic Relations what is interesting for you or foreign keys in abstract model classes.

Although notice that inheritance is always a is-a relationship while a normal foreign key usage implies a has-a relationship.

In your example, Customer should not inherit from Account as a customer has an account.

An inheritance example would be a Place which is either a Restaurant or a Cinema etc.

Edit after comment:

Well, there is a own section for this in the documentation:

Class inheritance and model managers aren't quite a perfect match for each other. Managers are often specific to the classes they are defined on and inheriting them in subclasses isn't necessarily a good idea. Also, because the first manager declared is the default manager, it is important to allow that to be controlled. So here's how Django handles custom managers and model inheritance:

...

  • Managers from abstract base classes are always inherited by the child class, using Python's normal name resolution order (names on the child class override all others; then come names on the first parent class, and so on). Abstract base classes are designed to capture information and behavior that is common to their child classes. Defining common managers is an appropriate part of this common information.
  • The default manager on a class is either the first manager declared on the class, if that exists, or the default manager of the first abstract base class in the parent hierarchy, if that exists. If no default manager is explicitly declared, Django's normal default manager is used.

I would only do if the inherited classes belong somehow to the same scope.
If you really a so many classes that it matters to add one line to these classes then you probably have not a good DB or application design.

And try not to put everything in one manager just to be able to use only one manager in a lot classes.

写给空气的情书 2024-08-26 07:28:00

在这种情况下,您将拥有 1 个包含帐户 ID 的客户表,如果您添加工作人员,他将拥有自己的包含帐户 ID 的表。

我想你可能想要一个包含帐户和附加对象客户、工人等的单表?这样您就永远不会混淆您的帐户。

In this case you'll have 1 table for customer with account ID and if you'll add Worker he'll have his own table with account ID.

I think you probably want to have single table with accounts and attached objects customers, workers etc? This way you'll never mix-up your accounts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文