Josue Thanks so much! That makes so much sense. I really appreciate it.
Sincerely, Chris On Wed, Sep 29, 2021 at 5:27 PM Josué Alvarez <josuetec2...@gmail.com> wrote: > > Try with: > > *global allowed* > *allowed* = [e.service.name for e in aservs] > ... > servs = [a.service for a in appts if (a.service in *allowed*)] > El lunes, 20 de septiembre de 2021 a las 8:46:07 UTC-6, cseb...@gmail.com > escribió: > >> 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 >> >> -- > 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+unsubscr...@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 > <https://groups.google.com/d/msgid/django-users/f4303d4b-5830-4331-b22d-0817f59b59d7n%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAG5-5iKqB_39Y8vM%2Bw4oS5cvvicnHAoV%3D0Ax1T7z0uWPoU7uXg%40mail.gmail.com.