Repository: django-extensions/django-extensions

Issue:
Admin Generator complains about "ManyToManyRel" in Django 1.8
My project has no many to many fields.

Error as follows;-

  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/shayne/.virtualenvs/nidhoggr/local/lib/python2.7/site-packages/django/core/management/**init**.py", line 338, in execute_from_command_line
    utility.execute()
  File "/home/shayne/.virtualenvs/nidhoggr/local/lib/python2.7/site-packages/django/core/management/**init**.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/shayne/.virtualenvs/nidhoggr/local/lib/python2.7/site-packages/django/core/management/base.py", line 390, in run_from_argv
    self.execute(_args, *_cmd_options)
  File "/home/shayne/.virtualenvs/nidhoggr/local/lib/python2.7/site-packages/django/core/management/base.py", line 441, in execute
    output = self.handle(_args, *_options)
  File "/home/shayne/.virtualenvs/nidhoggr/local/lib/python2.7/site-packages/django_extensions/management/utils.py", line 71, in inner
    ret = func(self, _args, *_kwargs)
  File "/home/shayne/.virtualenvs/nidhoggr/local/lib/python2.7/site-packages/django_extensions/management/commands/admin_generator.py", line 329, in handle
    self.handle_app(app, model_res, *_kwargs)
  File "/home/shayne/.virtualenvs/nidhoggr/local/lib/python2.7/site-packages/django_extensions/management/commands/admin_generator.py", line 332, in handle_app
    print(AdminApp(app, model_res, *_options))
  File "/home/shayne/.virtualenvs/nidhoggr/local/lib/python2.7/site-packages/django_extensions/management/commands/admin_generator.py", line 68, in **str**
    return self.**unicode**().encode('utf8')
  File "/home/shayne/.virtualenvs/nidhoggr/local/lib/python2.7/site-packages/django_extensions/management/commands/admin_generator.py", line 91, in **unicode**
    return ''.join(self._unicode_generator())
  File "/home/shayne/.virtualenvs/nidhoggr/local/lib/python2.7/site-packages/django_extensions/management/commands/admin_generator.py", line 101, in _unicode_generator
    class_=admin_model,
  File "/home/shayne/.virtualenvs/nidhoggr/local/lib/python2.7/site-packages/django_extensions/management/commands/admin_generator.py", line 68, in **str**
    return self.**unicode**().encode('utf8')
  File "/home/shayne/.virtualenvs/nidhoggr/local/lib/python2.7/site-packages/django_extensions/management/commands/admin_generator.py", line 197, in **unicode**
    return ''.join(self._unicode_generator())
  File "/home/shayne/.virtualenvs/nidhoggr/local/lib/python2.7/site-packages/django_extensions/management/commands/admin_generator.py", line 242, in _unicode_generator
    self._process()
  File "/home/shayne/.virtualenvs/nidhoggr/local/lib/python2.7/site-packages/django_extensions/management/commands/admin_generator.py", line 251, in _process
    self.raw_id_fields += list(self._process_many_to_many(meta))
  File "/home/shayne/.virtualenvs/nidhoggr/local/lib/python2.7/site-packages/django_extensions/management/commands/admin_generator.py", line 153, in _process_many_to_many
    related_objects = field.related.parent_model.objects.all()
AttributeError: 'ManyToManyRel' object has no attribute 'parent_model'



Discussion:

Resolving patch:
diff --git a/django_extensions/management/commands/admin_generator.py b/django_extensions/management/commands/admin_generator.py
--- a/django_extensions/management/commands/admin_generator.py
+++ b/django_extensions/management/commands/admin_generator.py
@@ -166,7 +166,8 @@ def name(self):
     def _process_many_to_many(self, meta):
         raw_id_threshold = self.raw_id_threshold
         for field in meta.local_many_to_many:
-            related_objects = field.related.parent_model.objects.all()
+            related_model = getattr(field.related, 'related_model', field.related.model)
+            related_objects = related_model.objects.all()
             if(related_objects[:raw_id_threshold].count() < raw_id_threshold):
                 yield field.name
 
@@ -181,7 +182,8 @@ def _process_foreign_key(self, field):
         raw_id_threshold = self.raw_id_threshold
         list_filter_threshold = self.list_filter_threshold
         max_count = max(list_filter_threshold, raw_id_threshold)
-        related_count = field.related.parent_model.objects.all()
+        related_model = getattr(field.related, 'related_model', field.related.model)
+        related_count = related_model.objects.all()
         related_count = related_count[:max_count].count()
 
         if related_count >= raw_id_threshold:


Test patch: