I am trying to use the generic CreateView class to handle forms for a set of models inherited from the same base class.
class BaseContent(models.Model): ... class XContent(BaseContent): ... class YContent(BaseContent): ...
To save DRY stuff, I want to define a single CreateView class that will handle all the inherited classes from BaseContent.
URL pattern for this view:
url(r'^content/add/(?P<model_name>\w+)/$', ContentCreateView.as_view(), name='content_add')
Something like this should work:
class ContentCreateView(CreateView): template_name = 'content_form.html' def get_model(self, request):
But I get this exception:
ContentCreateView is missing a queryset. Define ContentCreateView.model, ContentCreateView.queryset, or override ContentCreateView.get_object().
This suggestion does not seem to be fulfilled, because I do not want to set a class attribute, such as model or queryset , so that the model form is dynamically generated. Overriding get_object not like creating an object.
I tried to override get_queryset() , but this method does not accept the request parameter and does not have access to self.model_name , which comes from the url template.
In short, how can I get CreateView to use a dynamic form based on a parameter passed from a URL?
Thanks.