On May 26, 11:07 am, piker <da...@allnatives.com.au> wrote:
> I have a model class (A) that has a  M2M field with its relationship
> to another model (B). When I save an instance of model class (A) I
> want to automatically update a BooleanField in all the many, related
> model(B) instances at the same time. Is the right way to do this
> through a Model Method? If so, how should I reference across the M2M
> relationship? Any advice appreciated as I can't seem to find an answer
> to this specific problem.

Something like the code below (untested). If you want to update
depending on the current value of bool_field, have a look at
https://docs.djangoproject.com/en/dev/topics/db/queries/#filters-can-reference-fields-on-the-model.

class A(models.Model):
    ...
    def save(self, *args, **kwargs):
        super(A, self).save(*args, **kwargs)
        self.b_set.update(bool_field=True)

Cheers, Roald

-- 
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to