#12859: Incorrect docs on Updating multiple objects at once using related
records
---------------------------+------------------------------------------------
Reporter: dwillis | Owner: dwillis
Status: new | Milestone:
Component: Documentation | Version: 1.0
Keywords: | Stage: Unreviewed
Has_patch: 0 |
---------------------------+------------------------------------------------
The docs for [http://docs.djangoproject.com/en/dev/topics/db/queries
/#updating-multiple-objects-at-once Updating multiple objects at once] say
that "The only restriction on the QuerySet that is updated is that it can
only access one database table, the model's main table. So don't try to
filter based on related fields or anything like that; it won't work."
But I've been able to update a QuerySet based on a filter of related
records. You just can't ''update'' the related records. For example, given
the following models:
{{{
class CoachingJob(models.Model):
name = models.CharField(max_length=75)
slug = models.SlugField(max_length=75)
class CollegeCoach(models.Model):
coach = models.CharField(max_length=75)
jobs = models.ManyToManyField(CoachingJob)
start_date = models.DateField(null=True, blank=True)
end_date = models.DateField(null=True, blank=True)
is_head_coach = models.BooleanField(default=False)
}}}
I can perform the following update successfully:
{{{
CollegeCoach.objects.select_related().filter(jobs__name='Head
Coach').update(is_head_coach=True)
}}}
If I try to update the CoachingJob model, it properly raises a
FieldDoesNotExist error:
{{{
CollegeCoach.objects.select_related().filter(jobs__name='Head
Coach').update(jobs__name='Top Dog')
FieldDoesNotExist: CollegeCoach has no field named 'name'
}}}
The attached patch suggests changing the docs to say that filtering on
related records is permitted, but not updating those related records, and
provides an example.
--
Ticket URL: <http://code.djangoproject.com/ticket/12859>
Django <http://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 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-updates?hl=en.