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

  • Home
  • SEARCH
  • 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 7715205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:19:36+00:00 2026-06-01T02:19:36+00:00

I have a app to manage book orders from some people, here is the

  • 0

I have a app to manage book orders from some people, here is the model :

class Order(models.Model):
    person = models.ForeignKey(Person)
    book = models.ForeignKey(Book)
    qty = models.IntegerField(default=0)

I also use a ModelFormSet to allow people to order several books at once. Although the form use initial 0 values for the “qty” field, I’m afraid some people will use an empty string instead of it.
So, what I want is to “convert” this Null entry to 0 before the form is processed (but after it has been bound, obviously)
I found the following solution:

def order(request, person_id):
    if request.method == "POST":
        # Hack to correct Null entries from user... TODO: find better way...
        posted_datas = request.POST.copy()
        for p in [k for k,v in posted_datas.items() if re.compile("qty").search(k) and not v]:
            posted_datas[p] = 0
        formset = BookOrderFormSet(posted_datas)
        if formset.is_valid():
....

It works fine, and I feel it will probably help someone in the same situation, but i’m wondering if there is a better/cleaner way to go…

  • 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-06-01T02:19:37+00:00Added an answer on June 1, 2026 at 2:19 am

    edit
    So you want to forgive user who deletes default 0 and leaves the qty field empty w/o providing any suitable number, by reseting the value of the field to 0 w/o showing him the error “qty is required”? Normally, better not to do this IMHO =)

    But if you want, besides check-and-set it in view, you could also patch the clean method of form Field to not to raise exception for empty value but change it to the default instead. For example

    from django.core.validators import EMPTY_VALUES
    
    class SmoothIntegerField(IntegerField):
        def clean(self, value):
            if value in EMPTY_VALUES:
                value = self.initial # set to default value
            return super(SmoothIntegerField, self).clean(value)    
    

    ignore following prev misunderstanding answer

    I don’t think you need this extra work. Django form and models protects it well:

    • empty string ” or totally absence of qty will not get passed because the qty field is required
    • string which is not integer will not get passed because the IntegerField itself ensures coercion of nonempty value to integer
    • even you don’t use formset.is_valid to ensure above things, non-number such as null still cannot be in DB because there is constriction that qty column is not nullable
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Android app with manage a countdowntimer (class CountDownTimer) that it is
I am creating an app to manage homework, and have a class called Assignment.
Good day, I have created an object that will manage data access. My app
We have a managed app, that uses an assembly. That assembly uses some unmanaged
I have a managed class library (say mylib.dll) and a 3rd party managed app
We have App A as main app. Now we build from it App B
I have an app that basically can be used to download, upload, and manage
I have the following situation. Project files was stored in ~/django-apps/app-old/app. For some reasons
I have 3 models: customer book_manager book The customer can edit its own setting,
Relatively new rails programmer here, so bear with me. I have an app where

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.