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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:41:12+00:00 2026-05-15T02:41:12+00:00

So basically, I’ve got a rather large Django project going. It’s a private web

  • 0

So basically, I’ve got a rather large Django project going. It’s a private web portal that allows users to manage various phone-related tasks.

Several pages of the portal provide a listing of Model objects to users, and list all of their attributes in a HTML table (so that users can visually look through a list of these items).

The problem I’m having is: I cannot find a Django-ish or pythonic way to handle the sorting of these Model objects by field name. As an example of what I’m talking about, here is one of my views which lists all Partyline Model objects:

def list_partylines(request):
    """
    List all `Partyline`s that we own.
    """

    # Figure out which sort term to use.
    sort_field = request.REQUEST.get('sortby', 'did').strip()
    if sort_field.startswith('-'):
        search = sort_field[1:]
        sort_toggle = ''
    else:
        search = sort_field
        sort_toggle = '-'

    # Check to see if the sort term is valid.
    if not (search in Partyline._meta.get_all_field_names()):
        sort_field = 'did'

    if is_user_type(request.user, ['admin']):
        partylines = Partyline.objects.all().order_by(sort_field)
    else:
        partylines = get_my_partylines(request.user, sort_field)

    variables = RequestContext(request, {
        'partylines': partylines,
        'sort_toggle': sort_toggle
    })
    return render_to_response('portal/partylines/list.html', variables)

The sorting code basically allows users to specify a /url/?sortby=model_field_name parameter which will then return a sorted listing of objects whenever users click on the HTML table name displayed on the page.

Since I have various views in various apps which all show a listing of Model objects, and require sorting, I’m wondering if there is a generic way to do this sorting so that I don’t have to?

I’m sorry if this question is a bit unclear, I’m struggling to find the right way to phrase this question.

Thanks.

  • 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-15T02:41:13+00:00Added an answer on May 15, 2026 at 2:41 am

    The way that I’d look at doing this is through a custom QuerySet. In your model, you can define the class QuerySet and add your sorting there. In order to maintain all the logic in the model object, I’d also move the contents of get_my_partylines into the QuerySet, too.

    ## This class is used to replicate QuerySet methods into a manager.
    ## This way:  Partyline.objects.for_user(foo) works the same as
    ## Partyline.objects.filter(date=today).for_user(foo)
    class CustomQuerySetManager(models.Manager):
        def get_query_set(self):
            return self.model.QuerySet(self.model)
        def __getattr__(self, attr, *args):
            try:
                return getattr(self.__class__, attr, *args)
            except AttributeError:
                return getattr(self.get_query_set(), attr, *args)
    
    
    class Partyline(models.Model):
        ## Define fields, blah blah.
        objects = CustomQuerySetManager()
        class QuerySet(QuerySet):
            def sort_for_request(self, request):
                sort_field = request.REQUEST.get('sortby', 'did').strip()
                reverse_order = False
                if sort_field.startswith('-'):
                    search = sort_field[1:]
                else:
                    search = sort_field
                    reverse_order = True
    
                # Check to see if the sort term is valid.
                if not (search in Partyline._meta.get_all_field_names()):
                    sort_field = 'did'
    
                partylines = self.all().order_by(sort_field)
                if reverse_order:
                    partylines.reverse()
                return partylines
            def for_user(self, user):
                if is_user_type(request.user, ['admin']):
                    return self.all()
                else:
                    ## Code from get_my_partylines goes here.
                    return self.all() ## Temporary.
    

    views.py:

    def list_partylines(request):
        """
        List all `Partyline`s that we own.
        """
        partylines = Partylines.objects.for_user(request.user).sort_for_request(request)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 487k
  • Answers 487k
  • 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
  • Editorial Team
    Editorial Team added an answer You can check if the event target is an anchor,… May 16, 2026 at 8:29 am
  • Editorial Team
    Editorial Team added an answer I don't think you'll run into memory problems. I wrote… May 16, 2026 at 8:29 am
  • Editorial Team
    Editorial Team added an answer My reading of the help here is that the SSF_… May 16, 2026 at 8:29 am

Trending Tags

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

Top Members

Related Questions

No related questions found

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.