> I have two models, one of them has a many to many relation to the
> other one.
>
> I want to add some checks to the first model, so I overwrote the save
> method. If my checks are not ok, I plan not to call super().save(),
> but this gives me the error "'FirstModel' instance needs to have a
> primary key value before a many-to-many relationship can be used."
>
> As far as I understood, the problem is that since I don't create the
> entry in the db for the first model, the successive creation of the
> association between the two models fails.
>
> Is there a way to stop the creation of all entries when the save
> method in my first model is called?

You should consider using a custom ModelForm[1] instead of this
approach. Override its clean() method to perform your checks and throw
a ValidationError when you don't want the save to proceed. You can
then also make the Admin use this custom form[2].

[1] 
http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#the-save-method
[2] 
http://docs.djangoproject.com/en/dev/ref/contrib/admin/#adding-custom-validation-to-the-admin

-Rajesh D

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to