python - How to make class based view accept parameters from URL or hardcoded in URLconf -
i working on class based generic view takes model name argument , processes model name more parameters. had working fine when hardcoded model name entry in urlconf:
url(r'^generic/', resultcreateview.as_view(model = 'sometask'))
snippets of class based view:
class resultcreateview(createview): model = none #this here, expecting overwritten, because otherwise error saying can't pass in 'model' kwarg above because 'model' not attribute of class def __init__(self,*args, **kwargs): self.model = get_model_object_from_modelname(kwargs['model']) self.form_class = my_custom_function_to_generate_a_formclass(self.model) self.template_name = self.model.template #template_name attribute set on model class return super(resultcreateview,self).__init__(*args, **kwargs)
when tried switch passing model parameter in via url, i.e.:
url(r'^tasks/(?p<model>\w+)$', resultcreateview.as_view())
my custom init method no longer works. get:
resultcreateview missing queryset. define resultcreateview.model, resultcreateview.queryset, or override resultcreateview.get_queryset()
i can't figure out where/when 'model' argument gets passed in url pattern view class. ideally, able make view work in either case (hardcoded parameter in urlconf or parameter url pattern) don't know put code processing happens @ right time. right place put code, or there approach should using?
edit: (additional complication: need decorate view decorator takes 'model' argument.)
those parameters passed actual request handler methods, not __init__
method of view. so, in case of request:
class resultcreateview(createview): model = none def get(self, request, model_name): self.model = get_model_object_from_modelname(model_name) self.form_class = my_custom_function_to_generate_a_formclass(self.model) self.template_name = self.model.template #template_name attribute set on model class return super(resultcreateview,self).get(request, model_name)
Comments
Post a Comment