Oh yes, I should have known that:

* you must have inlines for this to work.
* the if line should be:   if formset.cleaned_data[0]['yourfield'] ==
yourfield_value:

end of update...


On Jun 22, 1:11 pm, hadaraz <[email protected]> wrote:
> Hi,
>
> I recently needed to override a manytomany field on the save_model()
> method in the admin. However, this is not possible because it will get
> overwritten later with the original values. I asked if this will
> change in the future on the django-developers 
> list:http://groups.google.com/group/django-developers/msg/180d491dc781be49
> Never got an answer...
> Anyway, here is how I solved it. If you look at django/contrib/admin/
> options.py, you will see that the order of saving is this:
>
> 1. save_model()
> 2. save_m2m()
> 3. save_formset()
>
> So, overriding the save_formset() method (I'm using inlines anyway,
> but I think it would work without them as well) enables me change the
> way manytomany behaves. Example use in my MyAdmin.py:
>
>  def save_formset(self, request, form, formset, change):
>         "override the manytomany field"
>         super(MyAdmin, self).save_formset(request, form, formset,
> change)
>         if formset.field == something:   # see note later on
>              # form.instance is your model
>              ...do something with form.instace
>              ....
>              form.instance.save()
>
> Note: this function will run as many times as the inline forms you
> have, so make sure it only happens once, maybe using an if statement
> to check something on your formset, as done above.
>
> hope it helps someone,
> hadaraz
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
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