0

I'm building out a Django admin's fieldsets with the following code:

new_fieldsets = [
    (None, {
        "fields": ("title", "slug", "parent"),
    }),
    ("Security", {
        "fields": ("requires_authentication", "hide_from_anonymous"),
    }),
    ("Publication", {
        "fields": ("publication_date", "expiry_date", "is_online"),
        "classes": ("collapse",)
    }),
    ("Navigation", {
        "fields": ("short_title", "in_navigation"),
        "classes": ("collapse",)
    })
]

new_fieldsets.extend(PageBaseAdmin.fieldsets)

removed_fieldsets = [
    new_fieldsets.index(PageBaseAdmin.TITLE_FIELDS),
    new_fieldsets.index(PageBaseAdmin.PUBLICATION_FIELDS),
    new_fieldsets.index(PageBaseAdmin.NAVIGATION_FIELDS)
]

fieldsets = [v for k, v in enumerate(new_fieldsets) if k not in removed_fieldsets]

Which throws me this error:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/dangamble/Envs/cms-testingP3/lib/python3.4/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/Users/dangamble/Envs/cms-testingP3/lib/python3.4/site-packages/django/core/management/__init__.py", line 312, in execute
    django.setup()
  File "/Users/dangamble/Envs/cms-testingP3/lib/python3.4/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/dangamble/Envs/cms-testingP3/lib/python3.4/site-packages/django/apps/registry.py", line 115, in populate
    app_config.ready()
  File "/Users/dangamble/Envs/cms-testingP3/lib/python3.4/site-packages/django/contrib/admin/apps.py", line 22, in ready
    self.module.autodiscover()
  File "/Users/dangamble/Envs/cms-testingP3/lib/python3.4/site-packages/django/contrib/admin/__init__.py", line 24, in autodiscover
    autodiscover_modules('admin', register_to=site)
  File "/Users/dangamble/Envs/cms-testingP3/lib/python3.4/site-packages/django/utils/module_loading.py", line 74, in autodiscover_modules
    import_module('%s.%s' % (app_config.name, module_to_search))
  File "/Users/dangamble/Envs/cms-testingP3/lib/python3.4/importlib/__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1471, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "/Users/dangamble/Workspace/other/cms/cms/apps/pages/admin.py", line 46, in <module>
    class PageAdmin(PageBaseAdmin):
  File "/Users/dangamble/Workspace/other/cms/cms/apps/pages/admin.py", line 83, in PageAdmin
    fieldsets = [v for k, v in enumerate(new_fieldsets) if k not in removed_fieldsets]
  File "/Users/dangamble/Workspace/other/cms/cms/apps/pages/admin.py", line 83, in <listcomp>
    fieldsets = [v for k, v in enumerate(new_fieldsets) if k not in removed_fieldsets]
NameError: name 'removed_fieldsets' is not defined

If i swap out the List comprehension for:

fieldsets = []

for k, v in enumerate(new_fieldsets):
    if k not in removed_fieldsets:
        fieldsets.append(v)

Everything runs along fine and dandy. It's also worth noting that this works in Python 2 just fine as well. Also i've cleared all my __pycache__ folders as well.

Been scratching my head and can't work out for the life of me what i've done wrong in the comprehension for it not to work on P3. I've done some googling and can't find any changes to the way enumerate or list comprehension changed in P3 so i'm pretty stumped..

Dan Gamble
  • 3,965
  • 1
  • 24
  • 44
  • 2
    List comprehensions have their own scope in Python 3, they cannot read variables present in class namespace. – Ashwini Chaudhary Jun 25 '15 at 13:53
  • 1
    https://docs.python.org/3/reference/executionmodel.html – Ashwini Chaudhary Jun 25 '15 at 13:53
  • So instead of doing in the Class namespace, i should be doing it in: `get_fieldsets()` function instead? – Dan Gamble Jun 25 '15 at 13:57
  • 1
    I don't use python3, but I seem to remember that it does not allow you to use the tuple that way before the `for`. Try with `(k, v)` or `t` as a whole tuple, then access k and v inside it with t[0] and t[] – Pynchia Jun 25 '15 at 13:58
  • The tuple change made no difference unfortunately, it's starting to look like it's what @AshwiniChaudhary mentioned about using variables in the Class namespace – Dan Gamble Jun 25 '15 at 14:01

0 Answers0