How to add extra context & queryset field in Django generic views?

sheshkovsky picture sheshkovsky · Oct 4, 2015 · Viewed 9.8k times · Source

I'm building Django application, with submitting links and voting functionality.

I want to show all links, voted by a user in user details page. I can retrieve them in python shell using this:

Link.objects.filter(votes__voter=user)

But I don't know how to add this to the view as a extra context field. Here are the codes:

models.py

class Link(models.Model):
    title       = models.CharField(max_length=200)
    submitter   = models.ForeignKey(User)
    submit_date = models.DateTimeField(auto_now_add=True)
    up_votes    = models.IntegerField(default=0, blank=True, db_index=True)
    ...

class UserProfile(models.Model):
    user        = models.OneToOneField(User, unique=True)
    ...

class Vote(models.Model):
    voter = models.ForeignKey(User)
    link = models.ForeignKey(Link, related_name='votes')
    ...

views.py

class UserProfileDetailView(DetailView):
    model       = get_user_model()
    slug_field  = "username"
    template_name = "user_detail.html"


    def get_object(self, queryset=None):
        user    = super(UserProfileDetailView, self).get_object(queryset)s
        UserProfile.objects.get_or_create(user=user)
        return user

user_detail.html

...
{% if object == request.user and request.user.is_authenticated %}
    <p><a href='{% url "edit_profile" %}'>Edit My Profile</a></p>
{% endif %}

{% if link in voted_list %}
    <p><a href='{% url "link_detail" %}'>{{ link.title }}</a></p>
{% endif %}
...

I couldn't figure out how to implement adding extra field according to this link. I don't know that I need to define new query set, define new object or just point to a new context. If you can light this point up for me, I would be much more appreciated.

Answer

Rahul Gupta picture Rahul Gupta · Oct 4, 2015

You can just override the get_context_data() function in your UserProfileDetailView to add the extra context.

Here, we will add an extra variable voted_links which contains all the links for which the current user has voted.

class UserProfileDetailView(DetailView):     

    def get_context_data(self, **kwargs):
        context = super(UserProfileDetailView, self).get_context_data(**kwargs) # get the default context data
        context['voted_links'] = Link.objects.filter(votes__voter=self.request.user) # add extra field to the context
        return context