What is the best way to have different profiles for different users in django?

In my application, I have students, teachers and staff. Employees do not need a profile, but teachers and students need a different profile. I would prefer not to implement all this on my own (middleware and much more), anyway, so that get_profile () returns a different profile depending on the role of the user?

+6
django django-profiles
source share
2 answers

With Django 1.1, which is currently in beta, I would use a proxy model.

class MyUser(User): class Meta: proxy = True def get_profile(self): if self.role == 'professor': return ProfessorProfile._default_manager.get(user_id__exakt=self.id) elif self.role == 'student': return StudentProfile._default_manager.get(user_id__exakt=self.id) else: # staff return None 

get_profile needs a caching code from the original and so on. But essentially you could do something like this.

With Django 1.0.x, you can implement derived classes based on User, but this can break code in other places. For such things, I like proxy classes that simply add python functionality without changing database models.

+7
source share

Are you happy http://docs.djangoproject.com/en/dev/topics/auth/#auth-profiles ?

This is a standard solution.

0
source share

All Articles