The right way to handle additional user data in Django?
I need to attach a significant number of additional properties to every user in my Django project. Some of these properties are simple CharFields, others are more complex ManyToManyFields. The trouble for me, is that in my digging around of ways to do this, I开发者_如何转开发've found two options: The user profile method explained in the documentation, and the user subclassing method I see floating around the internet.
They both look complicated, and I'd rather not choose one only to find out that I need to go back and switch everything to the other method after months of development, so I ask here. Which way is the right way?
I have always done it traditional way, which means user profile, because it was suggested in the docs, but this is not the very clearest and elegant solution. You must always handle the possibility of user not having a profile, which I don't like very much. In the next project I would like to use the subclassing and if only it is in stable version of Django, I will press co-workers in my company to try this. This seems much more natural.
If you want to stay safe, use proile. If you prefer to take a little risk and benefit from a much better solution, use subclassing. It was introduced for a reason - there were several talks about weaknesses of profile solution.
The recommended, and, it seems, easiest, is the user profile method.
I've chosen a different path: I had a Clients model, witch should be authenticated. Instead of extending the user model to adjust to my Clients model, I left them separated. And then, when I save a new Client, in the Client save() method I create a user for that client. Then I do the same for updating and deleting to keep them in sync. I tried the other two options, but each had a lot of cons, that's why I gave up on them. And this has been working for me very well.
Regards. Aldo.
Like aldux, I prefer to create a separated Model and let User model untouched.
精彩评论