Django

Code

Ticket #8170 (closed: duplicate)

Opened 2 years ago

Last modified 2 years ago

Managers should be inherited correctly from extended Models

Reported by: Archatas Assigned to: mtredinnick
Milestone: 1.0 Component: Database layer (models, ORM)
Version: SVN Keywords:
Cc: Triage Stage: Accepted
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

Change History

08/08/08 15:28:24 changed by mtredinnick

  • owner changed from nobody to mtredinnick.
  • needs_better_patch changed.
  • needs_tests changed.
  • needs_docs changed.

08/12/08 12:13:24 changed by jacob

  • stage changed from Unreviewed to Accepted.
  • milestone set to 1.0.

08/12/08 12:13:33 changed by jacob

  • component changed from Uncategorized to Database wrapper.

08/16/08 04:11:28 changed 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.

08/16/08 11:57:02 changed by mtredinnick

  • status changed from new to closed.
  • resolution set to duplicate.

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


Add/Change #8170 (Managers should be inherited correctly from extended Models)




Change Properties
Action