Opened 8 years ago

Closed 8 years ago

Last modified 5 years ago

#8170 closed (duplicate)

Managers should be inherited correctly from extended Models

Reported by: Archatas Owned by: mtredinnick
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: UI/UX:


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 8 years ago by mtredinnick

  • Needs documentation unset
  • Needs tests unset
  • Owner changed from nobody to mtredinnick
  • Patch needs improvement unset

comment:2 Changed 8 years ago by jacob

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

comment:3 Changed 8 years ago by jacob

  • Component changed from Uncategorized to Database wrapper

comment:4 Changed 8 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 8 years ago by mtredinnick

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

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

comment:6 Changed 5 years ago by jacob

  • milestone 1.0 deleted

Milestone 1.0 deleted

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