Code

Opened 4 years ago

Closed 4 years ago

Last modified 3 years ago

#14471 closed (fixed)

db.models.Manager regression - can't override some methods

Reported by: Lucky Owned by: nobody
Component: Database layer (models, ORM) Version: 1.2
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description (last modified by russellm)

I want to migrate my project from the django-1.1.2 to the django 1.2.3 and I have problem with custom model managers what overrides Manager.create() method. For example:

class Book(models.Model):
    title = models.CharField(max_length=10)
    author = models.ForeignKey(Author)
    objects = CustomManager()

class CustomManager(models.Manager):
    def create(self, **kwargs):
        # business logic here
        return super(CustomManager, self).create(**kwargs)

Django 1.2.3 uses the CustomManager.create() method on the 'objects' descriptor as expected.

Book.objects.create(author=a1, title='How to be smart')

In the django-1.1.2 the same behavior had related field 'book_set' descriptor .
But in the 1.2.3 CustomManager.create() does not called anymore on related field. The next code has different behavior:

author.book_set.create(title='How to program')

The difference in the implementation for create method in the RelatedManger class. In the 1.1.2 it was delegate result to the create() method of it superclass (CustomManager).

return super(RelatedManager, self).create(**kwargs)

http://code.djangoproject.com/browser/django/tags/releases/1.1.2/django/db/models/fields/related.py#L367

But the 1.2. delegates result to the result of .using(db), what is not superclass, but QuerySet.

return super(RelatedManager, self).using(db).create(**kwargs)

http://code.djangoproject.com/browser/django/trunk/django/db/models/fields/related.py#L423

Look for the actual test case in the attachement. I'm Sorry for my silly english.

Attachments (1)

tests.py (915 bytes) - added by Lucky 4 years ago.

Download all attachments as: .zip

Change History (6)

Changed 4 years ago by Lucky

comment:1 Changed 4 years ago by anonymous

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Summary changed from db.models.Model regression - can't override some methods to db.models.Manager regression - can't override some methods

comment:2 Changed 4 years ago by russellm

  • Description modified (diff)
  • milestone set to 1.3
  • Triage Stage changed from Unreviewed to Accepted

comment:3 Changed 4 years ago by russellm

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

(In [14389]) Fixed #14471 -- Corrected a regression in the use of methods on custom managers on related querysets. Thanks to Lucky for the report.

comment:4 Changed 4 years ago by russellm

(In [14390]) [1.2.X] Fixed #14471 -- Corrected a regression in the use of methods on custom managers on related querysets. Thanks to Lucky for the report.

Backport of r14389 from trunk.

comment:5 Changed 3 years ago by jacob

  • milestone 1.3 deleted

Milestone 1.3 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.