#25787: Existence checks for related database sets
-------------------------------------+-------------------------------------
Reporter: JustusAdam | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: 1.8
(models, ORM) | Keywords: ManyToMany, contains,
Severity: Normal | in
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
I have recently had to check for presence of a particular object in a set
of related objects (either ManyToMany or ForeignKey) and I found it
unnecessary cumbersome not to be able to use the
{{{in}}} operator.
I propose to add an implementation of the {{{__contains__}}} method to the
{{{ManyRelatedManager}}} object which executes something like the
following query:
{{{
def __contains__(self, obj):
return self.filter(id=obj.id).exists()
}}}
which would enable the following sample behaviour:
{{{
my_student = request.user.student
my_course = Course.objects.get(id=course_id)
is_teacher = my_student in my_course.teachers
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/25787>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/053.786827c8fb9d86ce9171b6fb5e740408%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.