Django中的抽象基类模型与代理模型 [英] Abstract base class model vs Proxy model in Django

查看:131
本文介绍了Django中的抽象基类模型与代理模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个控制面板,该面板将在Django中具有多个子应用程序.我的模型之一是application,它将具有重要的设置,例如namedescriptioninstall_pathid(以便我可以将特定的设置和配置值与此应用程序相关联.

I am building a control panel that will have multiple sub-applications in Django. One of my models is an application, which will have important settings like name, description, install_path and id (so that I can associate specific settings and configuration values to this application.

现在,我正努力尝试弄清楚如何声明此特定模型.每个application都将执行与其他应用程序完全不同的操作.一个可以管理特定的CMS设置,另一个可以管理我们的开发环境的密码重置.目标是将通用支持项目放在一个地方.

Right now I'm struggling with trying to figure out how to declare this particular model. Each application will do something completely different than each other application. One might manage specific CMS settings and another may handle password resets for our development environment. The goal is to get the common support items in one place.

每个应用程序的主要信息将相同.每个人都会有一个名称,描述等.不同之处在于它们的工作方式和使用的设置.但是,这些设置是在其自己的模型中,并具有通过外键返回到应用程序的链接.

The main information for each application will be the same. Each will have a name, description, etc. The difference will be what they do and what settings they use. The settings are in their own model though, with a link back to the application via foreign key.

我不确定哪种模型类型最适合我的用例.两者看起来都很有用,但是如果是这样,我假设我错过了其中一个(或两个)的一个方面.

I'm unsure which model type would be most appropriate for my use case. Both look like they'd be useful, but if that's the case, I'm assuming that I am missing an aspect of one (or both) of them.

我的问题是,使用application之间有什么区别"rel =" nofollow noreferrer>抽象基类模型与

My question is, what is the difference between declaring my applications using abstract base class models vs. proxy models?

推荐答案

这个人八个月都没有碰过.我应该知道得更多,但我会对此采取行动.

Nobody's touched this for 8 months. I should know better, but I'm going to take a stab at it.

很明显,您的第一个选择是完全不使用基类,并在每个模型上复制您的Field.我知道您没有问这个问题,但是对于其他正在看这篇文章的人来说,这是适合初学者的好方法.这很容易,并且模型的所有内容都在一个地方列出,而不是指向您的某些字段的代码中位于其他位置的另一个模型.

Your first option, obviously, is to not use base classes at all and duplicate your Fields on each model. I know you didn't ask about this, but for others looking at this post, it is a good way to go for beginners. It's easy, and everything for the model is listed in one place rather than pointing to another model located somewhere else in the code for some of your fields.

抽象基类可能是下一个最简单和下一个最常用的类.当您在两个或多个模型中有很多重复字段时,值得考虑.使用此方法,您无需在多个模型之间一遍又一遍地键入(或剪切和粘贴)字段.当您声明基类抽象时,该表实际上不会在数据库中构建.基类仅在构建子表时使用.这使您的数据库更简单并保持了性能,因为您不必建立与基类的关系,也不必使用联接来查询数据.您还可以将其他字段(属性)添加到每个子模型的子类中(代理模型不能).

Abstract base classes are probably the next easiest and next most commonly used. When you have a lot of duplication of fields across two or more models it is worth considering. Using this method you can eliminate the need to type (or cut and paste) fields over and over across multiple models. When you declare the base class abstract, the table is never actually built in the database. The base class is only used when the child tables are built. This keeps your database simpler and maintains performance because you don't have to build relationships to the base class and use joins to query data. You can also add additional fields (attributes) to the child classes on each of your child models(which proxy models cannot).

代理模型有点类似,因为您有一个基类或父类,但是两者之间有很大的不同.在所有模型都具有相同字段(属性)但对象可能具有不同类型"的情况下,将使用代理模型.例如,您可能有一个Cars的基类,并使用制造商作为您的类型.然后,您可能拥有福特,雪佛兰和本田的车型,它们都是Cars的代理车型.它们都具有相同的字段.为模型选择的管理器类真正使它们彼此不同.从数据库的角度来看,实际上只构建了一个表... Cars,其性能要比构建多个表更好,但是缺点是,您不能在不向模型中添加特定于制造商的字段的情况下将它们添加到整个Cars表中.

Proxy models are somewhat similar in that you have a base or parent class, but there are significant differences from there. You will use proxy models in situations where all of you models have the same fields (attributes), but you might have different "types" of objects. For instance you might have a base class of Cars, and use the manufacturer as your type. Then you may have Ford, Chevy and Honda models that are all proxy models of Cars. They all have the same fields. The manager class chosen for the model is what really makes them different from each other. From a database perspective, really only one table is built... Cars, leading to better performance than building multiple tables, but the drawback is you can't add manufactures-specific fields to the model without adding them to the entire Cars table.

通常,对于具有大量重复字段的模型,我建议从抽象基类开始.代理模型似乎是一个更具体的用例,但是如果您拥有用例并且一旦更精通的话,也可以使用代理模型.

In general I would recommend starting with Abstract Base Classes for models with lots of duplicate fields. Proxy models seem to be a more specific use case, but can be used as well if you have the use case and once you're more well-versed.

根据您的描述,我不清楚您的特定用例是否是100%清楚的,但希望我能给您足够的信息来决定最适合您自己的情况.

I'm not 100% clear on your specific use case based on your description, but hopefully I've given you enough information to decide what's best on your own.

这篇关于Django中的抽象基类模型与代理模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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