I’ve got a couple models like Listing, Image (one to many to listing), Category (one to many to listing), etc.
How can I have a form that deals with all these models? Also, how can I enforce non row specific validation like say: require that at least three images are passed/associated with a listing?
When using modelforms, I don’t think it is possible to reference more than one model per modelform. You could create a custom form from forms.Form to handle the validation. When this comes up for me, I usually just use two/three/four modelforms, because I like consistency, and it keeps management at the template level easier.
For your second question, you need to override the clean() method of the modelform you care about. Here’s an example of one I wrote recently:
There are docs on overriding clean as well:
https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#overiding-clean-on-a-model-formset
Edit:
By this, “I usually just use two/three/four modelforms” I mean I create modelforms for each model. I instantiate them at the view level and I send them as part of the context to the template:
if some of your models have the same column names, just use the prefix argument when you instantiate.
Docs on prefix are found here:
https://docs.djangoproject.com/en/dev/ref/forms/api/#prefixes-for-forms
then you can just pass request.POST into each of the modelforms without having to parse out fields individually.