I would recommend you take a look at
lost-theories.com source code. One anti pattern that django docs seems to recommend is use django.contrib.auth.models.User as your main user model, and put rest of user info in
yourapp.accounts.models.UserProfile or something.
I propose, django officially recommends it as a bad approach. What we should tell new comers to django is:
- create a FooUser in the yourapp.accounts.models
, something like this:
class FooUser(models.Model):
duser = models.ForeignKey(User) # django.contrib.auth.models.User
address = models.TextField()
_get_username(self): return
self.duser.username
_get_email(self): return self.duser.email
_set_email(self, email):
self.duser.email = email
self.duser.save()
username = property(_get_username)
email = property(_get_email, _set_email)
- throughout your code base, use a foreign key to yourapp.accounts.models.FooUser instead of django.contrib.auth.models.User
- infact for clarity, use variable name fuser whenever referring to FooUser, and duser when referring to django's User instance.
- go as far as set wrong value for AUTH_PROFILE_MODULE to catch some code calling get_profile()
- set up a middle ware:
class FooUserMiddleware(object):
def process_request(self, request):
if request.user.is_authenticated():
request.fuser = FooUser.objects.get(duser=request.user)
else: request.fuser = None
=> use request.fuser in all your views.
-
use the context_processor:
def context_processor(request):
d['fuser'] = request.fuser
return d
and use fuser in all your templates. You can access both fuser.username
/email and fuser.address.
This will go a long way making life of a lot of developers easier. You can have as many types of Users as you wont, not having to worry how to hack .get_profile() etc.
And it is actually correct. Imagine if I have to have a friendship relationship between users, I can not obviously monkeypath django contrib auth app, so I would put that many to many in UserProfile as per current django recommendation, and
user.get_profile().friends.all() sounds wrong. With this you can say fuser.friends.get(). Natural.
--
Amit Upadhyay
Vakao!
+91-9820-295-512