Upon extending the excellent django project django-profiles (found on https://bitbucket.org/ubernostrum/django-profiles/wiki/Home) I stumbled upon something which I have been trying to solve for days.
The idea is fairly simple. I have users, which have user profiles (object UserProfile, linked to User). Now I added a new object to my project, Employment, which also links to the User object.
class Employment(models.Model):
"""
A class to describe the relationship of employment, past or present, between an employee and an organization.
"""
def __unicode__(self):
return self.function
def get_absolute_url(self):
return "/recycling/employment/%i/" % self.id
user = models.ForeignKey(User)
organization = models.ForeignKey(Organization)
date_start = models.DateField('Start date of employment', help_text="Please use the following format: YYYY-MM-DD.", null=True, blank=True)
date_end = models.DateField('End date of employment', help_text="Please use the following format: YYYY-MM-DD.", null=True, blank=True)
function = models.CharField(max_length=100)
present_job = models.BooleanField()
class UserProfile(models.Model):
"""
A class representing a the profile of a user. Needs to be generic enough to involve both employees as employers.
"""
def _get_absolute_url(self):
return ('profiles_profile_detail', (), { 'username': self.user.username })
# Link person to user
user = models.ForeignKey(User, unique=True, null=True)
registration_date = models.DateField(auto_now_add=True)
Now I would like to have a page (detail view) where I can show the UserProfile and also all employment for a specific user.
I figured that adding extra_context is the way to go and this works eg.:
('^profiles/(?P<username>\w+)/$', 'profiles.views.profile_detail', {'extra_context': {'employment_list': Employment.objects.all(),}}),
The problem I am facing however is that I would like to have user-specific objects (thus filtering) and not just all().
One pitfall is that the django-profiles project still works with functions as views, not classes, so subclassing is not an option. Another attention point is that the view should not be cached, so that if a user adds another employment object and is redirected to the details page, this change should be reflected.
It would be nice to find a solution for this without adapting the django-profiles code itself…
Thanks for your help!
Found a way of doing this, turned out to be fairly simple. I created a new view which generates a list with a filter. Pass this list in extra_context to the view defined in the profiles application and done…
def detail_userprofile_view(request, username):
"""
Returns a detail view including both the User Profile and the Employments linked to the user.
"""
employment_list = Employment.objects.filter(user__username=username)
context_dict = { 'employment_list': employment_list,}
return profile_detail(request, username, extra_context=context_dict)
You could always write a new view. Something simple like:
urls
views
Now in your template you can go
{{ user.first_name }}or{{ profile }}or{{ employment.organization }}etc…