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

65 views
Skip to first unread message

Saqib Ali

unread,
Jan 12, 2013, 2:06:57 AM1/12/13
to django...@googlegroups.com

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.

Kashif Ali

unread,
Jan 12, 2013, 2:40:00 AM1/12/13
to django...@googlegroups.com
I will suggest to create UserProfile model instead of customizing User model. Add as many fields as you like in UserProfile model and use this new model in your application wherever you are referencing User model.
You have todo some settings in your settings file in this regards.

Kashif



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



--
-Kashif

Saqib Ali

unread,
Jan 12, 2013, 3:27:21 AM1/12/13
to django...@googlegroups.com
Kashif, if I do that, then there are more questions/issues:

1) Should user UserProfile be an extension of the User class? Or should it be a completely separate, independent class?

2) Whichever method I decide to use, I would like my other models to use as their foreign keys the same Users that are shown to me in the Admin interface. If I create and implement UserProfile, then I will be creating a separate object -- not the same one from the Admin interface. So how do I work around that?


- Saqib

Kashif Ali

unread,
Jan 12, 2013, 3:57:43 AM1/12/13
to django...@googlegroups.com
On Sat, Jan 12, 2013 at 1:27 PM, Saqib Ali <saqib....@gmail.com> wrote:
Kashif, if I do that, then there are more questions/issues:

1) Should user UserProfile be an extension of the User class? Or should it be a completely separate, independent class?

 
You can not extend User class/model as its not an abstract class. Rather it will be a separate, independent one. e.g.

from django.contrib.auth.models import User
class UserProfile(models.Model):
    user = models.ForeignKey(User, unique=True, related_name='profile')
    theme = models.ForeignKey(Themes,null=True,blank=True)

    def __str__(self):
        return self.user

    class Meta:
        ordering = ["user"]


2) Whichever method I decide to use, I would like my other models to use as their foreign keys the same Users that are shown to me in the Admin interface.
 
It will be same Users as its referencing to the same model. only difference is object reference to the object.

If I create and implement UserProfile, then I will be creating a separate object -- not the same one from the Admin interface. So how do I work around that?

Yes it will be separate object but that object also contains user object as well and you can access any of its fields. You can change its behavior by having customs forms for your models. For this you have to look at how to implement django forms.
model factory and form factory can also be use as per requirement if you can have a look at there docs.



--
-Kashif

Russell Keith-Magee

unread,
Jan 12, 2013, 4:23:46 AM1/12/13
to django...@googlegroups.com
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 %-)

Iñigo Medina

unread,
Jan 12, 2013, 6:29:17 AM1/12/13
to django...@googlegroups.com

On Fri, Jan 11, 2013 at 11:06:57PM -0800, Saqib Ali wrote:
>
> 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.

Standard way of solving that is using a custom User Profile related to User
class. I've used such approach and it has the benefits that you keep separated
both models and so they become more reusable. However it has extra cost at
database level.

If you are planning to use Django 1.5 you might extend the User class.

I�igo

bb...@yahoo.com

unread,
Jan 12, 2013, 12:13:40 PM1/12/13
to django...@googlegroups.com
I'll advice you take a look at the custom user models documentation. It is new in django 1.5 which will become the latest stable version in less than 2 weeks from now(?). It looks like the cleanest way imho. Sorry I don't have the link but am sure a simple search will lead you to the django doc page.

--------------------------
Sent from my mobile device
Reply all
Reply to author
Forward
0 new messages