Tuesday, June 21, 2011

saving related objects in overridden save method on model

I have these models:

class Widget(models.Model):
name

class Person(models.Model):
requested = models.ForeignKey(Widget, related_name='requested')
considered = models.ManyToManyField(Widget,
related_name='considered')

def save(self, *args, **kwargs):
if not self.considered.filter(id=self.requested.id):
self.considered.add(self.requested)

I'm trying to make sure that when a Person has requested a widget,
that value automatically gets selected added as a considered value
too. I find that this does work if I do this when no considered
values are selected:

>>> p = Person.objects.get(pk=1)
>>> p.save()

The relationship gets saved. But when I attempt in the Django admin,
the two fields are not sync'd. Any reason why this would be?

Thx.

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

No comments:

Post a Comment