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

Related Questions

I'm working on an intranet django project (not using GAE) for a company that
I need some opinions here. I'm working on a Django project using buildout to
I'm working on a Google App Engine project using Django. I noticed that for
I have a django project that I have been using for a while, but
I'm considering using Django for a project I'm starting (fyi, a browser-based game) and
I’ve got a brand new Django project. I’ve added one minimal view function to
I've got a Django product I'm using iPython to interact with. I'm trying to
Brand new to django. We have a legacy django project using django 0.96x that
I'm starting a new web app project using Django and Pinax. I want to
I want to create a rollback button in my django project using MySQLdb. I

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.