#8170 closed (duplicate)
Managers should be inherited correctly from extended Models
| Reported by: | Archatas | Owned by: | Malcolm Tredinnick |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | dev |
| 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 |
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) []
Change History (6)
comment:1 by , 17 years ago
| Owner: | changed from to |
|---|
comment:2 by , 17 years ago
| milestone: | → 1.0 |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
comment:3 by , 17 years ago
| Component: | Uncategorized → Database wrapper |
|---|
comment:4 by , 17 years ago
comment:5 by , 17 years ago
| Resolution: | → duplicate |
|---|---|
| Status: | new → closed |
This is just a dupe of the broader issues in #7154.
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.