What is different about "./manage.py shell" vs. normal Python shell that makes this list comprehension not work anymore?

38 views
Skip to first unread message

cseb...@gmail.com

unread,
Sep 20, 2021, 10:46:07 AM9/20/21
to Django users
I noticed a list comprehension bombs in the "./manage.py shell" shell that I think would
be ok in normal Python shell.  What is different about "./manage.py shell"
that makes it not like certain list comprehensions?

Here is the code erase that I run this way..."./manage.py shell < erase"

import bighelp.models

ACCT  = bighelp.models.Account
ASERV = bighelp.models.AccountService
APPT  = bighelp.models.Appointment

for e in ACCT.objects.all()[:1]:
        appts       = APPT.objects.filter(account = e)[:3]
        aservs      = ASERV.objects.filter(account = e)
        allowed     = [e.service.name for e in aservs]

        print(f"allowed is a list of strings => {allowed}\n\n")

        print(f"appts is a collection of Django objects => {appts}\n\n")

        print("This way of defining servs works...\n\n")

        servs       = []
        for a in appts:
                if a.service in allowed:
                        servs.append(a.service)
        print(f"servs   = {servs}\n\n")

        print("This way gives a mysterious error...\n\n")

        servs = [a.service for a in appts if (a.service in allowed)]




***THE OUTPUT****


../manage.py shell < ./erase
allowed is a list of strings => ['pet', 'cleaning']


appts is a collection of Django objects => <QuerySet [<Appointment: Appointment object (998)>, <Appointment: Appointment object (1463561)>, <Appointment: Appointment object (863)>]>


This way of defining servs works...


servs   = ['cleaning', 'pet', 'cleaning']


This way gives a mysterious error...


Traceback (most recent call last):
  File "../manage.py", line 8, in <module>
    django.core.management.execute_from_command_line(sys.argv)
  File "/usr/lib/python3/dist-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/usr/lib/python3/dist-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/lib/python3/dist-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/lib/python3/dist-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/usr/lib/python3/dist-packages/django/core/management/commands/shell.py", line 92, in handle
    exec(sys.stdin.read())
  File "<string>", line 26, in <module>
  File "<string>", line 26, in <listcomp>
NameError: name 'allowed' is not defined

Josué Alvarez

unread,
Sep 29, 2021, 6:27:48 PM9/29/21
to Django users

Try with:
 
global allowed
allowed     = [e.service.name for e in aservs]
...
servs = [a.service for a in appts if (a.service in allowed)]

Christian Seberino

unread,
Sep 29, 2021, 8:59:07 PM9/29/21
to django...@googlegroups.com
Josue

Thanks so much!  That makes so much sense.  
I really appreciate it.

Sincerely,

Chris

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/IYAr438-uPk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f4303d4b-5830-4331-b22d-0817f59b59d7n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages