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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:30:43+00:00 2026-05-15T09:30:43+00:00

I just started playing with Django today and so far am finding it rather

  • 0

I just started playing with Django today and so far am finding it rather difficult to do simple things. What I’m struggling with right now is filtering a list of status types. The StatusTypes model is:

class StatusTypes(models.Model):
    status = models.CharField(max_length=50)
    type = models.IntegerField()
    def __unicode__(self):
        return self.status
    class Meta:
        db_table = u'status_types'

In one admin page I need all the results where type = 0 and in another I’ll need all the results where type = 1 so I can’t just limit it from within the model. How would I go about doing this?

EDIT: I should have been a bit more clear. I have a model “Unit” which has a foreign key to to StatusTypes. The models are as follows:

class StatusTypes(models.Model):
    status = models.CharField(max_length=50)
    type = models.IntegerField()
    def __unicode__(self):
        return self.status
    class Meta:
        db_table = u'status_types'

class Unit(models.Model):
    name = models.CharField(unique=True, max_length=50)
    status = models.ForeignKey(StatusTypes, db_column='status')
    note = models.TextField()
    date_added = models.DateTimeField()
    def __unicode__(self):
        return self.name
    class Meta:
        db_table = u'units'

So now in the admin page for the unit model I want to limit the status to only those with type = 1. Based off of lazerscience response below I tried the following code:

from inv.inventory.models import Unit
from django.contrib import admin

class UnitAdmin(admin.ModelAdmin):
    def queryset(self, request):
        qs = super(UnitAdmin, self).queryset(request)
        return qs.filter(type=0)

admin.site.register(Unit, UnitAdmin)

But, it didn’t change the select box at all. I also tried printing the value of qs and nothing was outputted to my terminal so I’m wondering if I have to some how call queryset?

EDIT 2: It might not have been clear that I want to filter this for the status dropdown that is on the create page for the Unit model.

  • 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-15T09:30:44+00:00Added an answer on May 15, 2026 at 9:30 am

    EDIT:

    It turns out that ModelAdmin.formfield_for_foreignkey was the right answer in this situation:
    http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_for_foreignkey

    PREVIOUS ANSWER:

    Take a look at the list_filter attribute of ModelAdmin. That sounds more like what you want to me since it will create a nice interface for filtering on different criteria rather than arbitrarily restricting your queryset.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.