1

I am developing a django project. I created some apps, some of those are related to User model, for instance, I have a feeds app that handles user feeds, and another app that deals with extra user data like age, contacts, and friends. for each of these, I created a table that should be connected to the User model, which I using for storing and authenticating users.

I found two ways to deal with this issue. One, is through extending User model to be like this:

ModelName(User):
    friends = models.ManyToMany('self')
    .....

Two, is through adding a foreign key to the new table like this:

ModelName(models.Model):
    user = models.ForeignKey(User, unique=True)
    friends = friends = models.ManyToMany('self')
    ......

I can't decide which to use in which case. in other words, what are the core differences between both?

amr.negm
  • 183
  • 1
  • 13
  • see e.g. http://stackoverflow.com/questions/5452288/django-why-create-a-onetoone-to-userprofile-instead-of-subclassing-auth-user – second Nov 20 '11 at 17:48
  • I think this question is what I was searching for – amr.negm Nov 20 '11 at 17:55
  • No, I don't think you were searching at all. This question has *several* duplicates. – Ignacio Vazquez-Abrams Nov 20 '11 at 21:13
  • I don't like idea connecting profile model to user via user field because of extra separation one user entity to 2 different models with 2 identificators and bunches of fields. In case of inheriting you just need to override default User module with inherited new one. As result every user will have only one ID. – ramusus Nov 21 '11 at 11:24

1 Answers1

2

Either way will technically work. Subclassing the User model is effectively the same as subclassing models.Model and then including a user = models.OneToOneField(User) line.

That said, for what it's worth, the Django book opts for the models.Model route. I also agree that this is syntactically more straightforward.

I would also point you to the storing additional information about users section of the Django documentation, which will teach you about the AUTH_PROFILE_MODULE setting and the get_profile() method. It is very much best practice to use those.

Luke Sneeringer
  • 9,270
  • 2
  • 35
  • 32
  • I agree with @Luke, inheriting from the `User` model is not such a good idea. Creating a `UserProfile` model is much better practice. And with `get_profile()`, is as fast as any other solution. – juliomalegria Nov 20 '11 at 21:16
  • Inheriting from `User` is a terrible idea. But this answer does not explain *why*. – Ignacio Vazquez-Abrams Nov 20 '11 at 21:19
  • I don't see any particular reason why inheriting from `User` is an *intrinsically* terrible idea (Django allows model inheritance for a reason). I just think it's always better to follow the framework's published best practice, and in this case, there is already a Django way to do *exactly* what the OP wants. – Luke Sneeringer Nov 21 '11 at 01:17
  • I don't like idea connecting profile model to user via `user` field because of extra separation one user entity to 2 different models with 2 identificators and bunches of fields. In case of inheriting you just need to override default User module with inherited new one. As result every user will have only one ID. – ramusus Nov 21 '11 at 11:24