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 726405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:26:54+00:00 2026-05-14T06:26:54+00:00

I have a Django form that uses an integer field to lookup a model

  • 0

I have a Django form that uses an integer field to lookup a model object by its primary key. The form has a save() method that uses the model object referred to by the integer field. The model’s manager’s get() method is called twice, once in the clean method and once in the save() method:

class MyForm(forms.Form):
    id_a = fields.IntegerField()

    def clean_id_a(user_id):
        id_a = self.cleaned_data['id_a']
        try:
            # here is the first call to get
            MyModel.objects.get(id=id_a)
        except User.DoesNotExist:
            raise ValidationError('Object does not exist')

    def save(self):
        id_a = self.cleaned_data['id_a']
        # here is the second call to get
        my_model_object = MyModel.objects.get(id=id_a)

        # do other stuff

I wasn’t sure whether this hits the database two times or one time so I returned the object itself in the clean method so that I could avoid a second get() call. Does calling get() hit the database two times? Or is the object cached in the thread?

class MyForm(forms.Form):
    id_a = fields.IntegerField()

    def clean_id_a(user_id):
        id_a = self.cleaned_data['id_a']
        try:
            # here is my workaround
            return MyModel.objects.get(id=id_a)
        except User.DoesNotExist:
            raise ValidationError('Object does not exist')

    def save(self):
        # looking up the cleaned value returns the model object
        my_model_object = self.cleaned_data['id_a']

        # do other stuff
  • 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-14T06:26:54+00:00Added an answer on May 14, 2026 at 6:26 am

    No, the value wouldn’t be cached. Your second example is the right way to go.

    (The first snippet actually contains an error, in that nothing is returned from the clean method, so the id_a attribute would end up empty.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Django form that uses a different number of fields based on
I have a web report that uses a Django form (new forms) for fields
I have a django form that first validates its data through calling form.is_valid(). If
I need set field value, not passed to Django Form constructor. I have model
I am creating a form that uses MultipleChoiceField. The values for this field are
I have a ModelForm in my Django app that uses a forms.ModelMultipleChoiceField, which displays
I have a Django form that will add an entry to a database with
I am trying to enable sluggified URLs in Django of the form that SO
I have a form that takes a path as input, analyzes files and subfolders
I'm trying to write a Django-subclassed ModelChoiceField class that knows how to load itself,

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.