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

-- 
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 [email protected].
To post to this group, send email to [email protected].
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.

Reply via email to