Opened 10 years ago

Last modified 10 years ago

#23624 closed New feature

Regression in ManyToManyField with through, runtime-generated models — at Version 2

Reported by: Ludovico Magnocavallo Owned by: nobody
Component: Documentation Version: 1.7
Severity: Normal Keywords:
Cc: 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 Ludovico Magnocavallo)

I admit my situation is a corner case: an app interfacing with a legacy db, where I need to extend an abstract model to contextualize it to identical tables, named differently (eg obj_1 table, obj_2 table, etc.), and to make things worse the number of tables is only known at runtime.

I am then dynamically generating a module for each set of models, and while things work great in Django 1.6, in 1.7 ManyToManyField using a through model breaks.

I am attaching a sample models.py file that illustrates the issue. I have not yet found the time to delve into what's happening, I hope someone with a better understanding of all the related stuff steps in as it's something I have never looked into.

What happens is that the manager seems to ignore the through model, here is the traceback (actual call and models in the attached file):

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/ludo/Desktop/dev/venv/spritz/local/lib/python2.7/site-packages/django/db/models/manager.py", line 191, in all
    return self.get_queryset()
  File "/home/ludo/Desktop/dev/venv/spritz/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 887, in get_queryset
    return qs._next_is_sticky().filter(**self.core_filters)
  File "/home/ludo/Desktop/dev/venv/spritz/local/lib/python2.7/site-packages/django/db/models/query.py", line 691, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "/home/ludo/Desktop/dev/venv/spritz/local/lib/python2.7/site-packages/django/db/models/query.py", line 709, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "/home/ludo/Desktop/dev/venv/spritz/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1287, in add_q
    clause, require_inner = self._add_q(where_part, self.used_aliases)
  File "/home/ludo/Desktop/dev/venv/spritz/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1314, in _add_q
    current_negated=current_negated, connector=connector)
  File "/home/ludo/Desktop/dev/venv/spritz/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1138, in build_filter
    lookups, parts, reffed_aggregate = self.solve_lookup_type(arg)
  File "/home/ludo/Desktop/dev/venv/spritz/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1076, in solve_lookup_type
    _, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
  File "/home/ludo/Desktop/dev/venv/spritz/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1383, in names_to_path
    self.raise_field_error(opts, name)
  File "/home/ludo/Desktop/dev/venv/spritz/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1389, in raise_field_error
    "Choices are: %s" % (name, ", ".join(available)))
FieldError: Cannot resolve keyword u'a' into field. Choices are: a_s, id, name

Change History (3)

by Ludovico Magnocavallo, 10 years ago

Attachment: models.py added

models.py

comment:1 by Collin Anderson, 10 years ago

A trace back would also help, or a failing test-case.

Last edited 10 years ago by Collin Anderson (previous) (diff)

comment:2 by Ludovico Magnocavallo, 10 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top