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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:09:52+00:00 2026-05-30T11:09:52+00:00

So, my problem is this. I have a legacy MySQL database that I’m building

  • 0

So, my problem is this. I have a legacy MySQL database that I’m building a shiny new Django application over. For whatever reason, some fairly daft design decisions were made—such as all fields, no matter what they contain, being stored as varchars—but because since other systems that I’m not rewriting depend on that same data I can’t destructively change that schema at all.

I want to treat a certain field—the stock quantity on hand—as an integer, so that in my template I can check its amount and display a relevant value (basically, if there are more than 100 items available, I want to just display “100+ Available”).

The existing value for stock quantity is stored as, oddly, a varchar holding a float (as if it’s possible to have fractional amounts of an item in stock):

item.qty: u"72.0"

Now, I figure as a worst case I can use QuerySet.values(), and iterate over the results, replacing each stock quantity with an int() parsed version of itself. Something like …

item_list = items.values()
for item in item_list:
    item['qty'] = int(float(item['qty']))

… but won’t that cause my QuerySet to evaluate itself completely? I confess to being fairly ignorant of the process by which Django handles lazy execution of queries, but it seems like working with actual values would mean evaluating the query before it needs to.

So, am I complaining about nothing (I mean, it’s definitely evaluating these values in the template anyway), or is there a better way to do what I need to do?

  • 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-30T11:09:54+00:00Added an answer on May 30, 2026 at 11:09 am

    Yes, iterating through in the view would evaluate the entire queryset. That may or not be what you want – for example, if you’re paginating, the paginator limits the query automatically, so you don’t want to evaluate the whole thing separately.

    I would approach this in a different way, by calling a function in the template to format the data. You can either do this with a method on the model, if there’s just one field you want to format:

    class Item(models.Model):
        ...
        def get_formatted_qty(self):
            return int(float(self.qty))
    

    and call it:

    {{ item.get_formatted_qty }}
    

    Or, to make it more general, you can define a custom filter in your templatetags:

    @register.filter
    def format_value(value):
        return int(float(value))
    
    
    {{ item.qty|format }}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a legacy (solve this problem for us issue) app that communicates with
Ok I have this problem that I've never had before, it's really bugging me.
I have this problem in my ASP.NET application where I'm seeing some of my
I have this problem that my sites uses alot of ajax and when a
I have this problem decomposing a relation schema into a set of schemas that
We have a legacy VB6 application that updates itself on startup by pulling down
I have some legacy application logic that sends files to an attached printer using
I currently have a legacy database (SQL 2005) that generates hash strings for tokens.
I have a legacy VB6 application that was built using MSDE. As many client's
I have a legacy database that I am working with a basic column layout

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.