Django

Code

Ticket #7252 (closed: duplicate)

Opened 2 months ago

Last modified 2 months ago

Non-default managers inherited from an abstract model don't function.

Reported by: floguy Assigned to: nobody
Milestone: Component: Uncategorized
Version: SVN Keywords:
Cc: Triage Stage: Unreviewed
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

Given these models:

class NaiveHierarchyManager(models.Manager):
    def get_roots(self):
        return self.get_query_set().filter(parent__isnull=True)

class NaiveHierarchy(models.Model):
    parent = models.ForeignKey('self', null=True)
    
    tree = NaiveHierarchyManager()
    
    class Meta:
        abstract = True

class BlogPost(NaiveHierarchy):
    title = models.CharField(max_length = 128)
    body = models.TextField()
    
    objects = models.Manager()
    
    def __unicode__(self):
        return self.title

What happens is that the 'tree' manager gets assigned correctly to BlogPost, but the manager's 'model' attribute still points to NaiveHierarchy, which is not the correct model. Upon trying to use the manager through BlogPost, it gives an AttributeError, complaining that the attribute _join_cache doesn't exist on NaiveHierarchy (which it shouldn't).

I've attached a patch which fixes the problem in all cases, and I can't think of a reason why it wouldn't work, but it seems like the problem is better solved elsewhere. However, all attempts on my part to fix the problem elsewhere I ran into snags. (!Manager's .model isn't set by the time ModelBase.__new__ gets called, etc).

Attachments

inheritance_manager_fix.diff (0.5 kB) - added by floguy on 05/16/08 19:27:30.

Change History

05/16/08 19:27:30 changed by floguy

  • attachment inheritance_manager_fix.diff added.

05/22/08 13:57:04 changed by sebastian_noack

  • status changed from new to closed.
  • needs_better_patch changed.
  • resolution set to duplicate.
  • needs_tests changed.
  • needs_docs changed.

Duplicate of #7154


Add/Change #7252 (Non-default managers inherited from an abstract model don't function.)




Change Properties
Action