I just started playing with Django today and still find it quite difficult to do simple things. Now I am trying to filter a list of state types. StatusTypes Model:
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'
On one admin page, I need all the results, where type = 0, and on the other, I need all the results, where type = 1, so I can’t just limit it from within the model. How can i do this?
EDIT: I should have been more clear. I have a Unit model that has a foreign key to StatusTypes. 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 on the administration page for the device model, I want to limit the status only to those who have type = 1. Based on the answer of lazerscience 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)
. qs, , , - ?
EDIT 2: , , , Unit.