Tuesday, July 31, 2012

Re: Adding more data to Generic Views

On 31-7-2012 6:17, Lachlan Musicman wrote:

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

So reverse relations.

> 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()

You don't need any of these. In your template, simply call:
{{ person.certificate_set.fieldname }}

If you insist on having them available as you do above, they should be:
context['job_list'] = self.get_object().vacancy_set.all()
context['certificate_list'] = self.get_object().certificate_set.all()
context['claim_list'] = self.get_object().compensation_set.all()

Required reading:
https://docs.djangoproject.com/en/1.4/ref/models/relations/

Also, skipping the tutorial is not recommended:
<https://docs.djangoproject.com/en/1.4/intro/tutorial03/#use-the-template-system>

> 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?

Not really. The only thing that bugs me about them is that adding extra
context requires sub-classing so you end up with a views.py that solely
consists of sub-classed generic views with 3 lines of get_context_data().
--
Melvyn Sopacua

--
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