Someone e-mailed me a reply, and I thought I'd make it public before I reply.
What you could do is use manual transaction management and do something like: @transaction.commit_manually def my_view(request): ... try: my_form.save() except MyFormException: # thrown when validation fails, etc. transaction.rollback() finally: transaction.commit() That should allow you to create the PKs and then get out of there if there's a problem. On Jun 15, 9:23 am, Heleen <heleen...@gmail.com> wrote: > Hello, > > I have a situation where I would like to do some validation on a many- > to-many field in a model before it is saved. When the validation fails > the model should not be saved and result in an appropriate error. > However to be able to do anything with a many-to-many field the > primary key of the model should be available, therefore it seems that > the model should be saved first. > > In Forms there is the option to save with commit=False, but I could > not find anything similar to this for the Models save function. > Also, using a pre-save signal would not work, because the model is not > saved yet. A post-save signal doesn't work either because then the > model gets saved even in the cases that it shouldn't. Rewriting the > save function would be the best option, but because I cannot 'pre'- > save with commit=False this doesn't work either. > > Could anyone tell me if there is such a function to achieve what I > need or if there is a better way of doing this? > Thanks very much! > Heleen -- 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 at http://groups.google.com/group/django-users?hl=en.