简体   繁体   中英

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?

To extend User, would be better to have a class with several attributes, or break this class into several classes with few attributes? I´m using the django ORM now.

Say I have a class called Person that extends User, would be better:

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.

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.

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.

Whatever you choose, the real trick is to optimize the number of database queries. The django-debug-toolbar is helpful here.

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.

Also take a look at the defer method when retrieving a queryset. 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.

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 )

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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