Make User and Group extendable
|Reported by:||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||master|
|Severity:||Keywords:||database, user, group, extension|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
I'm developing a django application where I've additional data to store for users (e.g. a signature, settings, etc.). I solved this by creating a new model called UserProfile, which stores this data and has a ForeignKey user to the django User object. This sollution works fine for some small things but sometimes it's really copious, e.g. if I want to get the user data of all users that belong to a special group. In this case I had to do something like this:
group = Group.objects.get(id=123) users = group.user_set.all() profiles = UserProfile.objects.filter(user__id__in=[u.id for u in users])
Now I'm wondering whether extending the User and Group model like this would be possible:
# in models.py from django.contrib.auth.models import User, Group class MyUser(User): signature = forms.CharField() post_count = forms.IntegerField() class MyGroup(Group): def get_absolute_url(): return 'my_own_url'
# in settings.py DJANGO_USER_MODEL = 'models.MyUser' DJANGO_GROUP_MODEL = 'models.MyGroup'
signature and post_count then would be stored in the same database table as the normal user data (username, date_joined, etc.).