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