1

I've linked a UserProfile to a user. However, I do not like having to jump back and forth in the templates to get first_name, last_name, etc. I would like to use the User model for only auth-related stuff, and UserProfile for all information related to the user (mostly, the display of the user information).

I was wondering if someone could evaluate how my current UserProfile model is, and where I could improve it or where it is redundant, etc. Thank you.

class UserProfile(models.Model): user = models.ForeignKey(User, unique=True) network = models.ForeignKey(Network) location = models.CharField(max_length=100, blank=True) graduation = models.IntegerField(blank=True, null=True, choices=YEAR) headline = models.CharField(max_length=100, blank=True,) positions = models.ManyToManyField(Position, through ='Timestamp', blank=True) bio = models.TextField(max_length=1000, blank=True) @property def get_first_name(self): return self.user.first_name @property def get_last_name(self): return self.user.last_name def get_full_name(self): return self.user.get_full_name @property def get_date_joined(self): return self.user.date_joined 

1 Answer 1

1

Don't use the "get" prefix on properties. Also, don't forget that you can't query on properties, so you'll need to join to User for those. Perhaps you may want to add some custom queries to your manager for that.

Sign up to request clarification or add additional context in comments.

5 Comments

thanks for the comment and the pointer on not being able to query over those read-only fields. What would I need to add to be able to do something like UserProfile.objects.order_by('first_name','last_name')?
Thanks a lot. UserProfile.objects.order_by('-user__last_name')
what is the reason not to use the "get" prefix on properties?
Properties are not getters. They are read from and/or assigned to directly, so there is no "action".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.