Opened 10 years ago

Closed 10 years ago

Last modified 7 years ago

#8170 closed (duplicate)

Managers should be inherited correctly from extended Models

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


If I have a base model which has a manager and is inherited later, all managers in the inherited model are broken.

For example, I define the Vehicle model which should have the default manager objects and also a specific manager vehicles. Then Airplane extends Vehicle.

# -*- coding: utf-8 -*-
from django.db import models

class VehicleManager(models.Manager):

class Vehicle(models.Model):
    title = models.CharField(max_length=255)
    pos_x = models.IntegerField()
    pos_y = models.IntegerField()
    objects = models.Manager()
    vehicles = VehicleManager()
    class Meta:
        abstract = True

class Airplane(Vehicle):
    pos_z = models.IntegerField()

When I filter out air planes now by any field from the Vehicle model, I get an error message:

>>> models.Airplane.objects.filter(pos_x=2)
  File "C:\Python24\lib\site-packages\django\db\models\sql\", line 1238, in setup_joins
    cached_data = opts._join_cache.get(name)
AttributeError: 'Options' object has no attribute '_join_cache'

When I filter out air planes by any field from the Airplane model, I get another error message:

>>> models.Airplane.objects.filter(pos_z=2)
  File "C:\Python24\lib\site-packages\django\db\models\sql\", line 1215, in setup_joins
    raise FieldError("Cannot resolve keyword %r into field. "
FieldError: Cannot resolve keyword 'pos_z' into field. Choices are: pos_x, pos_y, title

After exploring further I found out that the model attribute of the managers equals to Vehicle instead of Airplane. If I reassign that after model creation, then the managers work as expected:

>>> Airplane.objects.model = Airplane
>>> Airplane.vehicles.model = Airplane
>>> Airplane.objects.filter(pos_x=2)
>>> Airplane.objects.filter(pos_z=2)
>>> Airplane.vehicles.filter(pos_x=2)
>>> Airplane.vehicles.filter(pos_z=2)

Change History (6)

comment:1 Changed 10 years ago by Malcolm Tredinnick

Owner: changed from nobody to Malcolm Tredinnick

comment:2 Changed 10 years ago by Jacob

milestone: 1.0
Triage Stage: UnreviewedAccepted

comment:3 Changed 10 years ago by Jacob

Component: UncategorizedDatabase wrapper

comment:4 Changed 10 years ago by Thejaswi Puthraya

Just wanted to inform that the patch last uploaded in #7154 by emulbreh on 07/11/08 is getting rid of the error produced with this ticket. They seem to be related...having a custom manager in an abstract class.

comment:5 Changed 10 years ago by Malcolm Tredinnick

Resolution: duplicate
Status: newclosed

This is just a dupe of the broader issues in #7154.

comment:6 Changed 7 years ago by Jacob

milestone: 1.0

Milestone 1.0 deleted

Note: See TracTickets for help on using tickets.
Back to Top