Saturday, January 12, 2013

Re: Best/Cleanest way to add fields to Class User in models.py?



On Sat, Jan 12, 2013 at 3:06 PM, Saqib Ali <saqib.ali.75@gmail.com> wrote:

In my models.py, I use django.contrib.auth.models.User as a foreign key in many of the classes I created.

The User class obviously has a lot of important features that any website developer will need to track and manage users to the website.
However, I wish the class had three additional members -- all BooleanFields: myBoolA, myBoolB, myBoolC.

What is the safest/cleanest/easiest/best way to achieve this functionality? Where should I add those members? It seems unwise to modify the Django source package itself.


As others have replied, one option is to use a profile class. Although Django has the concept of a UserProfile, this is being deprecated; but the broader idea -- a separate table with a OneToOneKey to the User model -- is still valid.

In Django 1.5 (due for release any week now), you can define your own custom User model. If you're happy with the base Django User, and just want to add a couple of fields, you can extend an AbstractUser base class and add your own fields.

Which one is the better option? That's really up to you. The more you put on your User model, the less reusable your code will be. However, putting data into separate tables will incur a retrieval cost. 

One way to answer this question is to ask the purpose of the boolean fields you want to add. Are these extra fields essential properties of what it means to be a User in your project, or are they descriptions of how one particular application will interact with a User object. If they're fundamental properties of a User, a custom User object may be appropriate. If they're the latter, a profile object may be a better approach.

However, ultimately, only you can confirm which approach is best for your own project. 

Yours,
Russ Magee %-)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate