Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7086561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:33:54+00:00 2026-05-28T07:33:54+00:00

I have a Django app with custom form fields, some of which have slow

  • 0

I have a Django app with custom form fields, some of which have slow operations in their constructors. I was surprised recently to find out that those constructors were getting called when Django itself was starting up, even before a user does something that requires that form in a view.

Why are they getting instantiated at server start?

Example:

urls.py:

from myapp.views import view1
...
url(r'^test$', view1.test),

views/view1.py:

class MyForm(ModelForm):
    class Meta:
        model = MyModel
    field1 = MyChoiceField()

class MyChoiceField(ChoiceField):
    def __init__(self, choices=(), required=True, widget=None, label=None,
             initial=None, help_text=None, *args, **kwargs):
    super(ChoiceField, self).__init__(required, widget, label, initial,
                                      help_text, *args, **kwargs)     
    self.choices = [(m.id, m.name) for m in ReallyLargeTableModel.objects.all()]     

If I set a break point inside that field constructor, then start up Django, it breaks the first time I request any page, even if the view in question does not need that form or field. The stacktrace leads back to the import line in urls.py.

Is this because I’m importing view1 in urls.py, instead of importing view1.test?

Edit: This isn’t Django specific, here is a test case the illustrates the behavior:

class Something():
    def __init__(self):
        print "Something __init__() called"

class UsesSomething():
    field = Something()

If you run this in the interactive terminal, it will print “Something init() called”. This was surprising to me because I have not actually instantiated a UsesSomething object.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:33:54+00:00Added an answer on May 28, 2026 at 7:33 am

    Because you instantiate the fields in the form definition, which is presumably being imported by one of your views.

    The field init is the wrong place to do this sort of dynamic initialization, for this exact reason. You want something that is called when the form is initialized: ie, the form’s __init__.

    That said, you don’t actually want to do this at all – you just need to use forms.ModelChoiceField, which takes a queryset and does the dynamic assignment of choices for you.

    class MyForm(ModelForm):
        field1 = forms.ModelChoiceField(queryset=ReallyLargeTableModel.objects.all())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a django app which is used for managing registrations to a survey.
I have a Django app which uses LDAP as the authentication backend. I'm not
I have a django app that uses a custom middleware module to create a
I have never actually worked for a company which is deploying a Django App
I have a page which uses jQuery for some AJAX-stuff und Django as main
I have a ModelForm in my Django app that uses a forms.ModelMultipleChoiceField, which displays
I have a Django app with an image field (a custom ThumbnailImageField type) that
I have an app that makes use of 'django-registration' a custom built model named
I have a Django app, which populates content from a text file, and populates
I have a django app which basically is just a photo album. Right now

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.