Hi Michal,

Thanks a lot. That was an eye-opener and a big help :)

Thanks.

On Friday, October 12, 2018 at 1:39:38 PM UTC+5:30, Michal Petrucha wrote:
>
> -----BEGIN PGP SIGNED MESSAGE----- 
> Hash: SHA512 
>
> On Fri, Oct 12, 2018 at 12:52:31AM -0700, Web Architect wrote: 
> > Hi, 
> > 
> > We are using Django for our ecommerce site. 
> > 
> > I have some confusion on when the code gets executed in Django. I have a 
> > django form with a choice field in module m1.py: 
> > 
> > class SomeForm(forms.Form): 
> >     field = forms.ChoiceField(choices=get_choices()) 
>
> The problem is here – you're calling get_choices, and passing the 
> result to the ChoiceField as a static list. If you pass get_choices as 
> a function, it will be called at runtime, whenever the form is 
> instantiated: 
>
> class SomeForm(forms.Form): 
>     field = forms.ChoiceField(choices=get_choices) 
>
> Good luck, 
>
> Michal 
>
> > 
> > 
> > def get_choices(): 
> >     return some choices 
> > 
> > 
> > In views.py, I have imported SomeForm: 
> > 
> > from app.m1 import SomeForm 
> > 
> > class SomeFormView(Formview): 
> >     form = SomeForm 
> >     ...rest of the view code 
> > 
> > 
> > When I run the django project using ./manage.py runserver, I am 
> surprised 
> > to see that get_choices gets executed. This is undesirable as I can have 
> a 
> > Django queryset in get_choices and I do not want unnecessary DB access. 
> > 
> > It would be really helpful if someone can shed some light on the above 
> and 
> > tell me what exactly is happening. How can I avoid get_choices getting 
> > executed during django start  or running? get_choices should only get 
> > executed when SomeFormView is accessed. 
> > 
> > Thanks. 
> -----BEGIN PGP SIGNATURE----- 
> Version: GnuPG v1 
>
> iQIcBAEBCgAGBQJbwFaVAAoJEHA7T/IPM/klm44QAJMjeGXjs7fzHaCpi7Sn16Ib 
> bHAyoluqmS5+Pw9yYsg3FaYJwx1JC6r/vB6Hv9WfBAI5cIzJa+k0e8T7PkuUg3Nc 
> ioG4izJWYC8F4MSXXynRVrt8UR4OfQrDw2tmXPXI++nf9GgMmN1Q/VJwVyWKUK8h 
> MD6L8ap0IIhhjOr3Nwvc1lsT68Hx4eEBmPcGa88FsHMUpox6ktIYmIeCq9WDIVQz 
> GKxMdRr+SUjcq7NeqqLL1davlL2YIKAaSdy1tNaxQ890u436MUkQ0L5FK2FtpKIh 
> ViqraEi/BaHWzZ6Dr6l6oG5QlrIHiG89VjYvYYo6qAtB67Yl5A6QnLY1YkMpiNkv 
> jZJt+u3ad4+qP5JQ0/SDYruyN2cjt6SVowQmAAxyGYbzFYMhWEz/NDopPXB/8q01 
> nr7o+N4azH696Ryr9bH5KEilrZvoxxxt9Zb+IGaJU5tfHs4A2Pzmw6aNmwMkRAVg 
> Mo0eaQuOu4zG6ix5X2wX7z+2MrGfBw0OJisOH3BzPOQ9CdHOaB0HuaveKQ39aVCL 
> vmGgPy+NnBr+EL/r1PC+sD9JjSFM6sD8+7fB1rvEtYsILj+DHHICYOZGaBGU34zm 
> TJjCHDSTp5Dx/p5mrt8Y9UJLf5U7C2Sz562R2ez3wKkrCqxGKN7fY7cq0oB01Cph 
> geJsa6+RNjRHpCbM1U/U 
> =7eA9 
> -----END PGP SIGNATURE----- 
>

-- 
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/242ffbae-150e-45ec-877f-0a983044258d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to