Code

Opened 4 years ago

Closed 4 years ago

Last modified 3 years ago

#12859 closed (fixed)

Incorrect docs on Updating multiple objects at once using related records

Reported by: dwillis Owned by: dwillis
Component: Documentation Version: 1.0
Severity: Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

The docs for 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.

Attachments (2)

12859.patch (1.3 KB) - added by dwillis 4 years ago.
12859.2.patch (1.3 KB) - added by timo 4 years ago.
tabs -> spaces for existing patch

Download all attachments as: .zip

Change History (8)

Changed 4 years ago by dwillis

comment:1 Changed 4 years ago by dwillis

  • Has patch set
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Status changed from new to assigned

comment:2 Changed 4 years ago by russellm

  • milestone set to 1.2
  • Triage Stage changed from Unreviewed to Accepted

Changed 4 years ago by timo

tabs -> spaces for existing patch

comment:3 Changed 4 years ago by timo

  • Triage Stage changed from Accepted to Ready for checkin

comment:4 Changed 4 years ago by russellm

  • Resolution set to fixed
  • Status changed from assigned to closed

(In [12515]) Fixed #12859 -- Clarified the documentation on using multiple tables with .update() calls. Thanks to dwillis for the report.

comment:5 Changed 4 years ago by russellm

(In [12520]) [1.1.X] Fixed #12859 -- Clarified the documentation on using multiple tables with .update() calls. Thanks to dwillis for the report.

Backport of r12515 from trunk.

comment:6 Changed 3 years ago by jacob

  • milestone 1.2 deleted

Milestone 1.2 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.