2

The Django documentation mentions in the Class-based generic views that the DetailView is composed from: View, SingleObjectMixin, and SingleObjectTemplateResponseMixin. I am experimenting with this, as I am interested in creating a generic view that will do an object_detail view with a ModelForm so that my model rows can be generated automatically.

To try to duplicate the DetailView I tried to create a class as follows:

from django.views.generic import list_detail, View
from django.views.generic.detail import (SingleObjectMixin,
    SingleObjectTemplateResponseMixin, BaseDetailView)

class formdisplay(View,SingleObjectMixin,SingleObjectTemplateResponseMixin): pass

When I use formdisplay instead of list_detail.object_detail I get the error

TypeError at /inpatient-detail/4/
__init__() takes exactly 1 non-keyword argument (2 given)

Any hints at how to do this?

Also, where is the documentation on how to write the import statements? I had to google to find what to import from as I couldn't find that in the documentation.

Thanks in advance, Steve

kd4ttc
  • 1,075
  • 1
  • 10
  • 28
  • How are you using `formdisplay` in your url conf? `formdisplay.as_view()`? Plus you might want to do what is already built-in to Django. Check `django.views.generic.edit` and their `FormView` classes. – Torsten Engelbrecht Jun 30 '11 at 05:07
  • Thanks. I was looking for a single line way to leverage my ModelForm. The object_detail function is nice but won't take a form as input. I see now that i can do what I want by rewriting object_detail for a ModelView or by using the class based generic views and replacing the get method. – kd4ttc Jul 02 '11 at 04:11

1 Answers1

2

As django's documentation on class-based generic view is still not very state-of-the-art, the best thing to get more information about them is to browse the source code; for create/update views this is a good start.

When inheriting from multiple classes/mixins you should also keep an eye on their order - if you look at django's source you'll see they put the Mixins before the other classes!

It's not totally clear to me, what you are trying to achieve, but if your goal is to display a form with data from an existing object, django.views.generic.update.UpdateView should be your friend!

Bernhard Vallant
  • 49,468
  • 20
  • 120
  • 148
  • Thanks for pointing to the source. I now see that list_detail.object_detail is actually a function and the comments about class views is really saying to move to the class based generic views that can be used with mixins. – kd4ttc Jul 02 '11 at 04:03
  • The answer is available on http://stackoverflow.com/q/6564068/820321 Thanks to everyone. The comments to the related questions allowed me to figure this out. Couldn't have done this without you! – kd4ttc Jul 03 '11 at 17:38