Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#26953 closed Bug (invalid)

Custom manager inheriting BaseManager.from_queryset() crashes with AssertionError

Reported by: Mikuláš Poul Owned by: nobody
Component: Database layer (models, ORM) Version: 1.9
Severity: Normal Keywords:
Cc: Loic Bistuer Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Tim Graham)

The unthinkable happened!

Traceback (most recent call last):
  File "manage.py", line 14, in <module>
    execute_from_command_line(sys.argv)
  File "{...}/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 350, in execute_from_command_line
    utility.execute()
  File "{...}/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 324, in execute
    django.setup()
  File "{...}/local/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "{...}/local/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "{...}/local/lib/python2.7/site-packages/django/apps/config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "{...}/budgetter-app/apps/expense/models.py", line 29, in <module>
    class Expense(models.Model):
  File "{...}/local/lib/python2.7/site-packages/django/db/models/base.py", line 283, in __new__
    new_class._prepare()
  File "{...}/local/lib/python2.7/site-packages/django/db/models/base.py", line 332, in _prepare
    ensure_default_manager(cls)
  File "{...}/local/lib/python2.7/site-packages/django/db/models/manager.py", line 45, in ensure_default_manager
    "Should never get here. Please report a bug, including your "
AssertionError: Should never get here. Please report a bug, including your model and model manager setup.

My model and manager setup

from django.db import models
from django.db.models.manager import BaseManager
from django.db.models.query import QuerySet

class ExpenseQuerySet(QuerySet):
   pass

class ExpenseManager(BaseManager.from_queryset(ExpenseQuerySet)):
    pass

class Expense(models.Model):
    objects = ExpenseManager()

Change History (5)

comment:1 by Mikuláš Poul, 8 years ago

Adding use_for_related_fields = True to ExpenseManager fixed the issue. But still...

comment:2 by Tim Graham, 8 years ago

Cc: Loic Bistuer added
Description: modified (diff)

There's no crash in Django 1.10 as of ed0ff913c648b16c4471fc9a9441d1ee48cb5420, however, I'm not certain about the correct resolution of this ticket. For example, should we add an additional test for this case?

I edited the description to leave only the minimal code necessary to reproduce the issue.

comment:3 by Simon Charette, 8 years ago

Does the crash also happen if managers.Manager is used instead of managers.BaseManager?

The later is not part of the public API and I would be tempted to close this ticket as invalid if it's usage triggers the crash now that this code path is gone in 1.10+.

comment:4 by Tim Graham, 8 years ago

Resolution: invalid
Status: newclosed
Type: UncategorizedBug

Using Manager rather than BaseManager doesn't crash on 1.9. Thanks Simon.

comment:5 by Tim Graham, 8 years ago

Summary: Should never get here. Please report a bug, including your model and model manager setup.Custom manager inheriting BaseManager.from_queryset() crashes with AssertionError
Note: See TracTickets for help on using tickets.
Back to Top