Tuesday, July 31, 2012

Re: Adding more data to Generic Views

So, in conclusion, Lachlan, you would want to do something like:

 def get_context_data(self,**kwargs):
        #Call the base implementation first to get a context
        context = super(PersonDetailView, self).get_context_data(**kwargs)
        #Add in a querysets
        context['job_list'] = Vacancy.complete.filter(person_id=self.kwargs[self.pk_url_kwarg])
        context['certificate_list'] = Certificate.objects.filter(person_id=self.kwargs[self.pk_url_kwarg])
        context['claim_list'] = Compensation.objects.filter(person_id=self.kwargs[self.pk_url_kwarg])
        return context

I have not tested this, but it gives you the idea. You'll want to filter the Vacancy, Certificate and Compensation objects by the person's ID. 

Good luck

Tervitades/Regards
Karl Sutt


On Tue, Jul 31, 2012 at 12:07 PM, Per-Olof Åstrand <p.o.aastrand@gmail.com> wrote:
Hi,

I just started to convert my small hobby-project into class-based views and struggled with similar things (but with a ListView instead).

The person object is in the context dictionary, so you need to refer to it by context['person'].

More generally, it is instructive to print context, kwargs and self.kwargs inside the get_context_data method. That would give you the information you need. It took me some time to realize that url-captured parameters are available in self.kwargs.

Per-Olof


On Tuesday, July 31, 2012 6:17:26 AM UTC+2, Lachlan Musicman wrote:
Hola,

I'm confused about adding extra content to a class based Generic View. I've read
the docs at file:///home/datakid/src/django-docs/topics/class-based-views.html 
and have written the code accordingly. 

I have Person, Certificate, Job and Compensation objects. The last three all 
have an FK (or M2M) back to a (or some) Person(s).

My DetailView subclass (which I've put in views.py - is there a better or more
correct place for it?)

class PersonDetailView(DetailView):
    context_object_name = "person"
    model = Person

    def get_context_data(self,**kwargs):
        #Call the base implementation first to get a context
        context = super(PersonDetailView, self).get_context_data(**kwargs)
        #Add in a querysets
        context['job_list'] = Vacancy.complete.all()
        context['certificate_list'] = Certificate.objects.all()
        context['claim_list'] = Compensation.objects.all()
        return context

But I don't want the full list of Vacancies, Certificates or Claims - I just 
want those that are linked to the person - how can I filter by these? I've tried
.filter(self.get_id)
.filter(self.request.get_id)
.filter(self.person.get_id)
.filter(self.request.person.get_id)
.filter(applicants__get_id__exact=self.get_id) (in the case of Vacancy) etc

How do I filter by the person object that is already in the context?
I know the answer is simple - I should wait until tomorrow when my brain is 
fresher, but I want to finish this off tonight if possible.

Of course, the other thing that I can't help but thinking is that at this point, the non-generic-view method of urls/views might be a simpler way to go. While Generic Views are quite versatile,  is there a point at which they are considered to restricting? 

L.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/6O_nqDwvAAEJ.

To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment