Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Class based views and form processing [RSS Feed]

#1 Jan. 10, 2011 00:53:57

J.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Class based views and form processing


Hello,

I am experimenting with the new class based views coming in 1.3 and am
having trouble figuring out when/where to put this code. I want to
populate a model's created_by field with the user of the current
request.

In the old views, I would have done something this:
obj = form.save(commit=False)
obj.created_by = request.user
obj.save()

Now I am using a generic CreateView class:
class ListCreateView(CreateView):
model = List
form_class = ListForm

I tried supplying an initial data dictionary by overriding
get_initial(), but this doesn't work because the `created_by` field
has editable=False so the form doesn't include it in the fields list.

Am I missing something simple? Probably :). Any help would be greatly
appreciated!

Thanks,
-Justin



--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Jan. 10, 2011 01:32:06

Łukasz R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Class based views and form processing


``CreateView`` includes ``ModelFormMixin``, which is responsible for
saving the model form:

def form_valid(self, form):
self.object = form.save()
return super(ModelFormMixin, self).form_valid(form)

So, you need to override that method in your subclass:

class ListCreateView(CreateView):
model = List
   form_class = ListForm

def form_valid(self, form):
self.object = form.save(commit=False)
self.object.created_by = request.user
self.object.save()
# Ok, so now you have a problem. Calling super will call
form.save() again.
# A dirty hack is to skip ModelFormMixin in MRO - *note* -
it's a dirty hack
return super(ModelFormMixin, self).form_valid(form)

Looks like it's an oversight in the API, unless someone can come up
with a cleaner solution.

On 10 January 2011 01:53, Justin <jlmurph...@gmail.com> wrote:
> Hello,
>
> I am experimenting with the new class based views coming in 1.3 and am
> having trouble figuring out when/where to put this code. I want to
> populate a model's created_by field with the user of the current
> request.
>
> In the old views, I would have done something this:
> obj = form.save(commit=False)
> obj.created_by = request.user
> obj.save()
>
> Now I am using a generic CreateView class:
> class ListCreateView(CreateView):
>    model = List
>    form_class = ListForm
>
> I tried supplying an initial data dictionary by overriding
> get_initial(), but this doesn't work because the `created_by` field
> has editable=False so the form doesn't include it in the fields list.
>
> Am I missing something simple? Probably :). Any help would be greatly
> appreciated!
>
> Thanks,
> -Justin
>

--
Łukasz Rekucki

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Jan. 10, 2011 12:29:52

Justin M.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Class based views and form processing


Thanks, Łukasz. I looked into overriding that method too, but it seems like
there should be a cleaner way. I almost feel like the ``ModelFormMixin``
should have an API similar to a ``ModelAdmin`` where one could override a
``save_model()`` method on the subclass.

The ``form_valid()`` method does a lot of work that could be separted out
(granted I don't know if more methods to override is a good thing!). The
docs say that the method "saves the form instance, sets the current object
for the view, and redirects to
get_success_url()<http://docs.djangoproject.com/en/dev/ref/class-based-views/#django.views.generic.edit.ModelFormMixin.get_success_url>
."

This might just be semantics, but in my mind the validation of the form and
the saving of the form should be separated. Let's see if anybody else has
some input and if not then I will bring this over to django-developers
and/or IRC.

http://docs.djangoproject.com/en/dev/ref/class-based-views/#django.views.generic.edit.ModelFormMixin.form_validThanks,
Justin

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 Jan. 12, 2011 21:42:09

Justin M.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Class based views and form processing


Hi, I still haven't found an answer to this problem so I am shamelessly
bumping the last updated date.

Thanks in advance for your help!

-Justin

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#5 Jan. 12, 2011 22:03:01

Skylar S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Class based views and form processing


instead of calling super, you could just call FormMixin.form_valid directly,
yes?

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#6 Jan. 12, 2011 22:05:49

Skylar S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Class based views and form processing


what's wrong with just defining form_valid sth like:


form_valid(self, form):
self.object = form.save(commit=False)
#ponies
self.object.save()
return HttpResponseRedirect(self.get_success_url())

does that not work?

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#7 Jan. 12, 2011 22:17:43

Justin M.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Class based views and form processing


Thanks, Skylar. You're right, it worked. Now I am just shaking my head and
saying "next time, I'll just read the source..."

-Justin

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » Class based views and form processing [RSS Feed]

Board footer

Moderator control

Enjoy the 24th of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support