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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:53:52+00:00 2026-05-28T07:53:52+00:00

When I build a model in Django, I add the following information: def __unicode__(self):

  • 0

When I build a model in Django, I add the following information:

def __unicode__(self):
  return self.some_field

class Meta:
  ordering = ['some_field_to_order_by']

Is it possible to set ordering based on a sort of if/else scenario?

For example, I have a customer model which, for sake of argument, has these fields:

first_name
last_name
company_name
is_company (boolean)

If the customer is a Company, I only add information to the company_name field and set is_company=True, leaving the other two blank. If the customer is a person, then I add information to the first_name and last_name fields, leaving the company_name blank and the is_company=False.

I want to sort these records by last_name if the is_company field is False and company_name if is_company is True.

Is this possible?

EDIT For an example (per request, sort of)

For my app, this customer table holds information regarding owners of security systems. Sometimes, a security system is installed in a residential setting. In this case, the owner of the system is a person — thus, I would enter the first_name and last_name into the customer record. Sometimes the system is installed in a commercial setting, therefore the owner is a company. For this I enter only the company_name field, leaving the other fields blank.

Now, when I provide an estimate for a NEW security system installation, I can provide the estimate to a new customer or an existing customer (existing customer, but a new location for them). When it is an existing customer, I have a drop down box that lists all existing customers (ALL RECORDS in the customer table).

THIS is where I want all the records to be ordered properly. As it is now, I get a jumbled mess of hundreds of records making it brutal to find the existing owner.

Hopefully this helps with what I’m trying to achieve.

  • 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-28T07:53:53+00:00Added an answer on May 28, 2026 at 7:53 am

    You can create a custom manager, which will do what you want.

    class CompanyFilter(models.Manager):
       def get_query_set(self):
            qs = super(CompanyFilter, self).get_query_set()
            return qs.filter(is_company=True).order_by('company_name')
    
    class PersonFilter(models.Manager):
       def get_query_set(self):
            qs = super(PersonFilter, self).get_query_set()
            return qs.filter(is_company=False).order_by('last_name')
    
    class Contact(models.Model):
       # ... your regular fields
    
       companies = CompanyFilter()
       people = PersonFilter()
    
    
    all_companies = Contact.companies.all()
    all_people = Contact.people.all()
    all_contacts = Contact.objects.all()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following form: class ModuleItemForm2(forms.ModelForm): class Meta: model = Module_item fields =
I have two Django model classes that are structured similar to the following: class
I have a meta class for the Django User model that I use to
I'd like to build a custom admin action into a model defined by Django
Say I have two models in my Django project. class Project(models.Model): name = models.CharField(max_length=256)
I need to add a FULLTEXT index to one of my Django model's fields
Before I was just using the build-in django serializers and it added a model
hi i am using the following models to build a database from django.db import
I build a list of Django model objects by making several queries. Then I
Context: So, I am attempting to build a ridiculously complex domain model. Talking with

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.