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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:58:41+00:00 2026-05-10T15:58:41+00:00

In my Django project I am using Product.objects.all().order_by(‘order’) in a view, but it doesn’t

  • 0

In my Django project I am using Product.objects.all().order_by('order') in a view, but it doesn’t seem to be working properly.

This is it’s output:

Product Name Sort Evolution 2 Polarity 1 Jumbulaya 3 Kalidascope 4

It should look like this:

Product Name Sort Polarity 1 Evolution 2 Jumbulaya 3 Kalidascope 4

But it doesn’t. Any ideas?

My view (for that output):

def debug(request):     order = Product.objects.all().order_by('order')     return render_to_response('cms/debug.html', {'order' : order, 'name' : name}) 

And the view responsible for saving the order field:

def manage_all(request):  if request.method == 'POST':     PostEntries = len(request.POST)     x = 1            while x < PostEntries:         p = Product.objects.get(pk=x)         p.order = int(request.POST.get(str(x),''))         print 'Itr: ' + str(x)         x = x + 1     p.save()     print 'Product Order saved'          return HttpResponse('Saved') 

And the model (without the boring bits):

class Product(models.Model):     name = models.CharField(max_length=100)     order = models.IntegerField(blank = True, null = True 

Here is a ‘live’ example of the page http://massiveatom.com:8080/debug/ Please note that that is only running on the dev server, so it may not always be up.

I have asked in #django and they didn’t seem to know what was going on. One thought was that the database/Django was being confused by the SQL command it is generating (select * from table where 1 order by 'order'), but I would prefer not to change the order field in the model.

And I know there should be back-ticks surrounding order in the above SQL command, but the syntax parsing thingy kinda hated on it…

Edit: Each object has the correct value, so I don’t really know why it isn’t sorting it properly.

Edit 2: I don’t know what was going on, but it turns out putting p.save() in the loop fixed it all…

  • 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. 2026-05-10T15:58:41+00:00Added an answer on May 10, 2026 at 3:58 pm

    Your saving loop is wrong. You save Product outside of the loop. It should be:

    if request.method == 'POST':     PostEntries = len(request.POST)     x = 1                while x < PostEntries:             p = Product.objects.get(pk=x)             p.order = int(request.POST.get(str(x),''))             print 'Itr: ' + str(x)             x = x + 1             p.save() # NOTE HERE <- saving in loop instead of outside     print 'Product Order saved'                  return HttpResponse('Saved') 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 68k
  • Answers 68k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer First of all, the 'else if' statement is incorrect, since… May 11, 2026 at 12:18 pm
  • added an answer The problem is that, accessing an item in an array… May 11, 2026 at 12:18 pm
  • added an answer The feature is called covariance/contravariance and will be supported in… May 11, 2026 at 12:18 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.