views:

21

answers:

1

Say I have a Sale model which is use to track sales made, with a sold_on field. I want to have two view in Admin, one showing all Sale and one showing Sale in last week.

I cant override ModelAdmin.queryset, as I want to see both the Querysets at different times. What is the best way to do this.

+1  A: 

One way to go about this would be to (1) declare a proxy model for Sale (2) add a custom ModelAdmin for this proxy and (3) limit the queryset of this custom admin to return the sales from last week alone. Something like this:

# models.py
class LastWeekSales(Sale):
    class Meta:
        proxy = True
        verbose_name_plural = "Sales from last week"

# admin.py
class ProxySaleAdmin(SaleAdmin):
    def queryset(self, request):
        return self.model.objects.filter(**conditions)

admin.site.register(LastWeekSales, ProxySaleAdmin)

This way you will see a new item Sales from last week in your admin page that lists only the last week's sales.

Manoj Govindan