I am trying to use 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 keep things DRY, I want to define one CreateView class that will
handle all inherited classes from BaseContent.
The url pattern for that view is:
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):
# 'content' is the name of the application; model_name is
'xcontent', 'ycontent', ...
return ContentType.objects.get_by_natural_key('content',
self.model_name)
But I am getting this exception:
ContentCreateView is missing a queryset. Define
ContentCreateView.model, ContentCreateView.queryset, or override
ContentCreateView.get_object().
This suggestion does not seem to hold as I am not willing to set a
class attribute like `model` or `queryset` to keep the model form
generated dynamic. Overriding the `get_object` does not seem relevant
for creating an object.
I tried overriding `get_queryset()` but this method does not accept
the `request` parameter, nor have access to `self.model_name` which
comes from the url pattern.
So, (how) can I make a CreateView use a dynamic form based on a
parameter passed from the url?
Thanks,
oMat
--
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