views:

15

answers:

1

Hi,
I'm writing a hockey database/manager. So I have the following models:

class Team(models.Model):
   name = models.CharField(max_length=60)

class Game(models.Model):
   home_team = models.ForeignKey(Team,related_name='home_team')
   away_team = models.ForeignKey(Team,related_name='away_team')

class SeasonStats(models.Model):
   team = models.ForeignKey(Team)

Ok, so my problem is the following. There are a lot of teams, but Stats are just managed for my Club. So if I use "list_display" in the admin backend, I'd like to modify/overwrite the queryset which builds the sidebar for filtering, to just display our home teams as a filter option.
Is this somehow possible in Django?

I already made a custom form like this

class SeasonPlayerStatsAdminForm(forms.ModelForm):
   team = forms.ModelChoiceField(Team.objects.filter(club__home=True))

So now just the filtering is missing. Any ideas?

A: 

Interesting question. The first thing to do with this sort of thing is to look at the source, which is usually very well-commented.

It looks like ModelAdmin has a method get_changelist, which returns a class to use to create the changelist page. By default, that simply returns the ChangeList class from django.contrib.admin.views.main. In turn, that class has a method get_filters which appears to be the one that returns the values for the filter sidebar.

So, what you would need to do is to create a ChangeList subclass with an overwritten get_filters method that only returns the values you want. Then, in your ModelAdmin subclass, overwrite get_changelist to return your ChangeList subclass (note, you need to return the class itself, not an instance).

Daniel Roseman