Overriding Class-based View Form Validation

234 views
Skip to first unread message

Greg Back

unread,
Dec 10, 2014, 4:00:56 PM12/10/14
to django-d...@googlegroups.com
Hello-

I was wondering if it would be reasonable to modify the `django.views.generic.edit.ProcessFormView` class, such that the `post()` function calls a (hypothetical) `self.validate_form(form)` function rather than directly calling `form.is_valid()`. The default implementation of `validate_form()` would of course be to call `form.is_valid()`, to maintain backwards compatibility.

Background:

I am writing an application where a model (I'll call it `Thing`) has a ForeignKey to User and a CharField ("name"), among some other fields. I would like "name" to be unique for any given user; I'm using `unique_together` in `class Meta:` to accomplish this. I have a CreateView subclass which allows users to create new Things; the `user` field is excluded from the form, and automatically set in the View. I was previously doing this in the `form_valid` function, but realized that this is being called after the form is already validated (without a User). By the time the object is saved to the database, the `user

Greg Back

unread,
Dec 10, 2014, 5:26:35 PM12/10/14
to django-d...@googlegroups.com
Sorry, I accidentally sent this before finishing the background. What I meant to say:

I am writing an application where a model (I'll call it `Thing`) has a ForeignKey to User and a CharField ("name"), among some other fields. I would like "name" to be unique for any given user; I'm using `unique_together` in `class Meta:` to accomplish this. I have a CreateView subclass which allows users to create new Things; the `user` field is excluded from the form, and automatically set in the View (extracting from `self.request`). I was previously doing this in the `form_valid` function, but realized that this is being called after the form is already validated (without a User). By the time the object is saved to the database, the `user` field is set, so the database raises an IntegrityError. 

I hacked around this by overriding `get_form` to call the parent class, then adding the request.user to the form.instance. This works, but has the side effect of also being called during GET requests, which doesn't break anything but is suboptimal.

Does that all make sense? Am I missing a more obvious way of doing this?

Thanks,
Greg

Curtis Maloney

unread,
Dec 11, 2014, 6:12:53 PM12/11/14
to django-d...@googlegroups.com
Firstly, I think this query really belongs in django-users  ... this list is about development _of_ django, not _with_ django.

Secondly, why not override get_form_kwargs [1] to add the user into creating the form class?

But yes, validation all belongs inside the form class, ideally.


--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/3353afdb-7070-49bb-9d45-ea072ac9bbb5%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Greg Back

unread,
Jan 2, 2015, 10:42:29 AM1/2/15
to django-d...@googlegroups.com
Thanks, Curtis.

Sorry for not replying sooner. I'm not sure why I didn't see your response initially. 

I wasn't aware of get_form_kwargs. I'll have to see if that will be sufficient for what I'm trying to do. 

I debated between django-users and django-developers, but decided on this because I was actually proposing modifying some of Django's classes themselves; if get_form_kwargs works, I obviously wouldn't need to do that.

Thanks again
Greg
Reply all
Reply to author
Forward
0 new messages