tags:

views:

141

answers:

2

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.

+2  A: 

You can override the queryset-method on your 'ModelAdmin':

from django.contrib import admin

class MyModelAdmin(admin.ModelAdmin):

    def queryset(self, request):
        qs = super(MyAdmin, self).queryset(request)
        return qs.filter(type=0)

admin.site.register(StatusTypes, MyModelAdmin)

This admin will only display you objects of your model that have type=0!

lazerscience
Thanks for the help. After you responded I realized how should have given more information in the question since what I was asking wasn't quite what I needed to do. I did try to modify your code to suite my needs as well as lookup a few resources on overriding queryset. I tried to print the value of qs but nothing got outputted to my terminal. Do I have to somehow call queryset?
blcArmadillo
You should then filter your queryset like this: `qs.filter(status__type = 0)`. At the end you're talking about a dropdown, what do you mean with that? And yes, as long as you have something saved, you should see something in your queryset. Try `python manage.py shell`, `from myapp.models import Unit`, `Unit.objects.filter(status__type=0)`; play around with that a bit!
lazerscience
Thanks for the help. I'll look into that.
blcArmadillo
I tried adding qs.filter(status__type = 0) but that didn't seem to work. I also tried using the shell like you suggested but got empty results back which makes sense. I then realized that I think I was confusing people. I'm trying to limit the options for the status select box on the Unit add page. Does that change things or should the code you originally posted still work for this?
blcArmadillo
+1  A: 

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.

Gabriel Hurley
Thanks for the response. List_filter won't work because you can't have a unit with a status of type = 0 and unless I'm mistaken list_filter just adds a sidebar giving the user the option. I'm kinda surprised this is so difficult to do... at least I haven't been able to find an answer yet. All I essentially want to do is add 'WHERE part = 1' to the query.
blcArmadillo
Maybe I'm missing the point on what you want, but if you add `status` to the `list_filter` list, it'll give you a widget that allows you to filter based on any allowable values for that field. It operates by adding a get parameter to the admin url, exactly like what you need.
Gabriel Hurley
Gabriel, I just realized what the confusion might be. I want to filter the status types for the status select field on the add page. Not add a filter column to the list of Units that have already been added. Does this help clarify things?
blcArmadillo
hmmm... I think what you might be looking for then is the formfield_for_foriegnkey method. see the docs here: http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_for_foreignkey
Gabriel Hurley
That is exactly what I was looking for. Thank you so much for the help.
blcArmadillo