Re: Can I override get_object() of a generic view?
Hey,
I'm not really sure what you're trying to do here. You're using a CreateView (which is built for creating new objects) and overriding the get_object(self, queryset=None) method (found in SingleObjectMixin -- https://github.com/django/django/blob/master/django/views/generic/detail.py) to, I'm assuming, dynamically set a Model and/or Queryset?
If you can get away with this (not saying it's impossible -- just not putting a ton of thought into it at the moment), you may have better luck giving it a shot in __init__().
I'm not sure on your use-case but looks pretty interesting none-the-less! Good luck!
On Tue, May 15, 2012 at 4:53 PM, Jon Paugh <jpaugh@gmx.us> wrote:
Hello.
How do I override get_object() from a subclass of CreateView? Is there something I'm missing?
I'm trying to override get_object() to provide a simple behavior. However, my get_object() is never called. Instead, Django complains that I didn't set self.model properly.
I opened the Python shell with ./manage.py shell and looked at the Create.__mro__ field to make check the method resolution order, and everything looked in order. Is something wrong here?
Thanks,
Jon
-- Here's my class
class Create(CreateView):
template_name='forms/ajax.html'
def get_object(self):
#raise Exception('Your face!')
if 'model' in self.kwargs:
self.model = self.kwargs['model']
elif 'queryset' in self.kwargs:
self.queryset = self.kwargs['queryset']
return super(Create, self).get_object(queryset)
-- And here's the mro.
Create.__mro__ = (dj.views.ajax.Create,
django.views.generic.edit.CreateView,
django.views.generic.detail.SingleObjectTemplateResponseMixin,
django.views.generic.base.TemplateResponseMixin,
django.views.generic.edit.BaseCreateView,
django.views.generic.edit.ModelFormMixin,
django.views.generic.edit.FormMixin,
django.views.generic.detail.SingleObjectMixin,
django.views.generic.edit.ProcessFormView,
django.views.generic.base.View,
object)
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/VjMAqWbA3dMJ.
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.
--
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.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home