Code

Opened 6 years ago

Closed 6 years ago

Last modified 3 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:

Description

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):
    pass

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\query.py", 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\query.py", 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)
[]

Attachments (0)

Change History (6)

comment:1 Changed 6 years ago by mtredinnick

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

comment:2 Changed 6 years ago by jacob

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

comment:3 Changed 6 years ago by jacob

  • Component changed from Uncategorized to Database wrapper

comment:4 Changed 6 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 6 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 3 years ago by jacob

  • milestone 1.0 deleted

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