简体   繁体   English

Django模型中的属性数

[英]Number of attributes in Django Models

I searched a lot and did not find what I´am looking for. 我搜索了很多东西,却没有找到我想要的东西。

What would be the best concept for a model class in django? 在Django中,模型类的最佳概念是什么?

To extend User, would be better to have a class with several attributes, or break this class into several classes with few attributes? 要扩展User,最好有一个带有多个属性的类,或者将这个类分解为几个带有几个属性的类? I´m using the django ORM now. 我现在正在使用django ORM。

Say I have a class called Person that extends User, would be better: 假设我有一个名为Person的类可以扩展User,那会更好:

class Person(models.Model):
    user = foreingkey(User)
    attribute1 =
    ...
    attributeN =

Or, would it be better to do this: 或者,这样做会更好:

class PersonContac(models.Model):
    user = foreingkey(User)
    attribute1 =
    ...
    attribute3 =

class PersonAddress(models.Model):
    user = foreingkey(User)
    attribute1 =
    ...
    attribute3 =

class PersonHobby(models.Model):
    user = foreingkey(User)
    attribute1 =
    ...
    attribute3 =

My each of my views would use the data from the smaller classes (probably). 我的每个视图都将使用较小类的数据(可能)。

Over time, the atrribute number can expand. 随着时间的流逝,at亵人数会扩大。

I want to do is do it once, and touch the minimum possible. 我想做的是一次,然后触摸最小的可能性。 Various attributes can be unfilled by the user, they are not required. 用户可以填写各种属性,而并非必需。 The number of user is indefinite (can be a lot). 用户数量是不确定的(可以很多)。

I´m concerned in terms of long term performance and maintaining. 我对长期性能和维护感到担忧。

If someone can explain me, what would be better for my code, and why. 如果有人可以解释我,那对我的代码会更好,以及为什么。 And what would be better in general (less classes/more attributes, or more classes/less attributes), using the Django ORM. 使用Django ORM,总的来说会更好(更少的类/更多的属性,或者更多的类/更少的属性)。

It is better if my views use the data of only one model class, or it makes no (or little) difference? 最好是,我的视图仅使用一个模型类的数据,或者没有(或几乎没有)区别?

Edit: 编辑:

On the rush for writing I used bad names on class. 在急于写作的时候,我在课堂上使用了坏名字。 None of these attributes are many-to-many fields, the User will have only one value for each attribute, or blank. 这些属性都不是“多对多”字段,“用户”每个属性只有一个值,即为空白。

The number of atributes can expand over time, but not in a great number. 属性的数量可以随着时间的流逝而扩展,但数量却不多。

Put any data that is specific to only one User directly in the model. 将仅特定于一个用户的任何数据直接放入模型中。 This would probably be things like "Name", "Birthday", etc. 可能是“名称”,“生日”之类的东西。

Some things might be better served by a separate model, though. 不过,使用单独的模型可能会更好地解决某些问题。 For example multiple people might have the same Hobby or one User might have multiple Hobby(s). 例如,多个人可能具有相同的兴趣爱好,或者一个用户可能具有多个兴趣爱好。 Make this a separate class and use a ForeignKeyField or ManyToManyField as necessary. 使它成为一个单独的类,并在必要时使用ForeignKeyField或ManyToManyField。

Whatever you choose, the real trick is to optimize the number of database queries. 无论您选择什么,真正的诀窍是优化数据库查询的数量。 The django-debug-toolbar is helpful here. django-debug-toolbar在这里很有帮助。

Splitting up your models would by default result in multiple database queries, so make sure to read up on select related to condense that down to one. 默认情况下,拆分模型将导致多个数据库查询,因此请务必仔细阅读精简相关的select

Also take a look at the defer method when retrieving a queryset. 检索查询集时,还要看看defer方法。 You can exclude some of those fields that aren't necessary if you know you won't use them in a particular view. 如果您知道不会在特定视图中使用这些字段,则可以排除其中一些不必要的字段。

I think it's all up to your interface. 我认为这完全取决于您的界面。

If you have to expose ALL data for a user in a single page and you have a single, large model you will end up with a single sql join instead of one for each smaller table. 如果必须在单个页面中公开用户的所有数据,并且只有一个大型模型,则最终将只有一个sql联接,而不是每个较小的表一个。

Conversely, if you just need a few of these attributes, you might obtain a small performance gain in memory usage if you join the user table with a smaller one because you don't have to load a lot of attributes that aren't going to be used (though this might be mitigated through values ( documentation here ) 相反,如果只需要其中一些属性,则将用户表与较小的属性连接在一起,则可能会在内存使用方面获得较小的性能提升,因为您不必加载很多不需要使用的属性。被使用(尽管可以通过values来缓解( 此处提供文档

Also, if your attributes are not mandatory, you should at least have an idea of how many attributes are going to be filled. 同样,如果您的属性不是强制性的,则至少应了解要填充多少个属性。 Having a large table of almost empty records could be a waste of space. 拥有几乎空的记录的大表可能会浪费空间。 Maybe a problem, maybe not. 也许是一个问题,也许不是。 It depends on your hw resources. 这取决于您的硬件资源。

Lastly, if you really think that your attributes can expand a lot, you could try the EAV approach . 最后,如果您真的认为自己的属性可以扩展很多,则可以尝试EAV方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM