Wednesday, February 1, 2012

Re: ManyRelatedManager reference

On Feb 1, 2:02 pm, "Demetrio Girardi" <demetrio.gira...@gmail.com>
wrote:
> I can't find a reference for ManyRelatedManager in the django docs. I have
> a few questions that you can ignore if there is in fact a reference somewhere
> and you can point me to it.
>
> If my model is
>
> class Model(models.Model):
>         many = models.ManyToManyField(OtherModel)
>
> what does this do?
>         Model.objects.filter(many = instance_of_other_model)
>
> why do these not work, and what is the correct way to do it?
>         instance_of_model.many = another_instance.many
>         instance_of_model.many.add(another_instance.many)

Something like instance_of_model.many.add(another_instance.many.all())
might work.

> If I have two instances of OtherModel, how do construct a queryset that
> matches all instances of Model that reference both?

I think, but I am not sure, that you should do two chained .filter()
calls. Something like:
qs.filter(many__pk=other_instance1.pk).filter(many__pk=other_instance2.pk).
No guarantees of that doing anything sane :)

- Anssi

--
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