tkhyn / django-gm2m

MIT License
36 stars 23 forks source link

exception in manage.py makemigrations with explicit related models #1

Closed tkhyn closed 9 years ago

tkhyn commented 9 years ago

Original report by Rod Morison (Bitbucket: rmorison, GitHub: rmorison).


I get this only when adding explicit models to the fields constructor, e.g.,

#!python

    listing = GM2MField(Production, Episode, related_name="genres")

If I remove the args to the constructor, makemigrations works.

#!python

File "/home/rod/pyves/slata/slata/slata/manage.py", line 10, in <module>
  execute_from_command_line(sys.argv)
File "/home/rod/pyves/slata/lib/python3.4/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
  utility.execute()
File "/home/rod/pyves/slata/lib/python3.4/site-packages/django/core/management/__init__.py", line 377, in execute
  self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/rod/pyves/slata/lib/python3.4/site-packages/django/core/management/base.py", line 288, in run_from_argv
  self.execute(*args, **options.__dict__)
File "/home/rod/pyves/slata/lib/python3.4/site-packages/django/core/management/base.py", line 338, in execute
  output = self.handle(*args, **options)
File "/home/rod/pyves/slata/lib/python3.4/site-packages/django/core/management/commands/makemigrations.py", line 111, in handle
  convert_apps=app_labels or None,
File "/home/rod/pyves/slata/lib/python3.4/site-packages/django/db/migrations/autodetector.py", line 40, in changes
  changes = self._detect_changes(convert_apps, graph)
File "/home/rod/pyves/slata/lib/python3.4/site-packages/django/db/migrations/autodetector.py", line 108, in _detect_changes
  self.new_apps = self.to_state.render()
File "/home/rod/pyves/slata/lib/python3.4/site-packages/django/db/migrations/state.py", line 67, in render
  model.render(self.apps)
File "/home/rod/pyves/slata/lib/python3.4/site-packages/django/db/migrations/state.py", line 311, in render
  body,
File "/home/rod/pyves/slata/lib/python3.4/site-packages/django/db/models/base.py", line 171, in __new__
  new_class.add_to_class(obj_name, obj)
File "/home/rod/pyves/slata/lib/python3.4/site-packages/django/db/models/base.py", line 300, in add_to_class
  value.contribute_to_class(cls, name)
File "/home/rod/pyves/slata/lib/python3.4/site-packages/gm2m/fields.py", line 171, in contribute_to_class
  self.rel.contribute_to_class(cls)
File "/home/rod/pyves/slata/lib/python3.4/site-packages/gm2m/relations.py", line 672, in contribute_to_class
  rel.contribute_to_class()
File "/home/rod/pyves/slata/lib/python3.4/site-packages/gm2m/relations.py", line 276, in contribute_to_class
  add_lazy_relation(self.fields.model, self, self.to,
File "/home/rod/pyves/slata/lib/python3.4/site-packages/gm2m/relations.py", line 269, in __getattribute__
  return sup(name)

builtins.AttributeError: 'GM2MUnitRel' object has no attribute 'fields'
tkhyn commented 9 years ago

Original comment by Rod Morison (Bitbucket: rmorison, GitHub: rmorison).


Looks like a typo. Not worth a pr...

#!python

+++ gm2m/relations.py   2014-12-04 14:43:07.420305898 -0800
@@ -273,7 +273,7 @@
             def resolve_related_class(rel, model, cls):
                 rel.to = model
                 rel.do_related_class()
-            add_lazy_relation(self.fields.model, self, self.to,
+            add_lazy_relation(self.field.model, self, self.to,
                               resolve_related_class)
         else:
             self.do_related_class()
tkhyn commented 9 years ago

Original comment by Thomas Khyn (Bitbucket: tkhyn, GitHub: tkhyn).


Thanks for spotting that, it's indeed a typo.

This bug should actually only happen when any of the provided models does not have a primary key yet, or is provided as a string. I'll add a test case to cover that situation.

tkhyn commented 9 years ago

Original comment by Thomas Khyn (Bitbucket: tkhyn, GitHub: tkhyn).


Adds failing test re #1

tkhyn commented 9 years ago

Original comment by Thomas Khyn (Bitbucket: tkhyn, GitHub: tkhyn).


Fixes #1 (typo)

tkhyn commented 9 years ago

Original comment by Thomas Khyn (Bitbucket: tkhyn, GitHub: tkhyn).


Fixed in v.0.2.2