How to filter objects by values of foreign objects in Django admin

135 views
Skip to first unread message

9dev...@gmail.com

unread,
Jul 7, 2014, 10:47:11 AM7/7/14
to django...@googlegroups.com

There are tables Product and Transaction. In Product admin there is a field that shows how many transactions have been created with this product.

Now, I want to filter the period of time in which these transactions were created. For example, there was 30 transactions in the last week, but 100 in the last month.

class Transaction(models.Model):
    created = models.DateTimeField()
    product = models.ForeignKey('Product')

class Product(models.Model):
    name = models.CharField()

    def num_of_trans(self):
        return Transaction.objects.filter(created="""DATE_VALUE_HERE""").filter(product=self.id).count()

I tried to adjust the queryset in a class that extends SimpleListFilter, however I did not manage to make it work.

Another thing I thought of was to add an additional parameter to num_of_trans function, but it seems there is not way to pass this parameter while using the filter.

I would appreciate any tips.

Shubham Pansari

unread,
Jul 7, 2014, 12:03:28 PM7/7/14
to django...@googlegroups.com
Instead of self.id you should use self itself as using self.id will not work.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c1612d89-d28b-4c82-b17d-703c43cc581d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

9dev...@gmail.com

unread,
Jul 7, 2014, 12:12:58 PM7/7/14
to django...@googlegroups.com
Thanks for pointing it out.

However, my question is about a different issue.

Lachlan Musicman

unread,
Jul 7, 2014, 12:21:23 PM7/7/14
to django...@googlegroups.com
For Range of dates:

https://docs.djangoproject.com/en/dev/ref/models/querysets/#range

for "greater than" or "less than" dates (and what that means), look at
the filter docs:


On 7 July 2014 22:12, <9dev...@gmail.com> wrote:
> Thanks for pointing it out.
>
> However, my question is about a different issue.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/fa5bbf2c-c699-4ef2-8b4e-85b14206d7f6%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.



--
The idea is that a beautiful image is frameable. Everything you need
to see is there: It’s everything you want, and it’s very pleasing
because there’s no extra information that you don’t get to see.
Everything’s in a nice package for you. But sublime art is
unframeable: It’s an image or idea that implies that there’s a bigger
image or idea that you can’t see: You’re only getting to look at a
fraction of it, and in that way it’s both beautiful and scary, because
it’s reminding you that there’s more that you don’t have access to.
It’s now sort of left the piece itself and it’s become your own
invention, so it’s personal as well as being scary as well as being
beautiful, which is what I really like about art like that.
-----------------------------------------------------------------------------------------------------------
Adventure Time http://theholenearthecenteroftheworld.com/

Lachlan Musicman

unread,
Jul 7, 2014, 12:22:07 PM7/7/14
to django...@googlegroups.com

9dev...@gmail.com

unread,
Jul 7, 2014, 12:28:44 PM7/7/14
to django...@googlegroups.com
Thank you, but I think you misunderstood.

I do not want to filter Product objects. I want to filter Transaction objects.

Please, take a look at provided example.

Tom Evans

unread,
Jul 7, 2014, 1:03:57 PM7/7/14
to django...@googlegroups.com
On Mon, Jul 7, 2014 at 11:47 AM, <9dev...@gmail.com> wrote:
> There are tables Product and Transaction. In Product admin there is a field
> that shows how many transactions have been created with this product.
>
> Now, I want to filter the period of time in which these transactions were
> created. For example, there was 30 transactions in the last week, but 100 in
> the last month.
>
> class Transaction(models.Model):
> created = models.DateTimeField()
> product = models.ForeignKey('Product')
>
> class Product(models.Model):
> name = models.CharField()
>
def num_of_trans(self, start_date=None, end_date=None):
qs = self.transaction_set.all()
if start_date:
qs = qs.filter(created__gt=start_date)
if end_date:
qs = qs.filter(created__lt=end_date)
return qs


Does that suffice?

Cheers

Tom

9dev...@gmail.com

unread,
Jul 7, 2014, 1:10:04 PM7/7/14
to django...@googlegroups.com, teva...@googlemail.com
No, it does not. I cannot pass any parameters to this function, because it is being invoked automatically by the admin.

class MyAdmin(admin.ModelAdmin):
    list_display = ['num_of_trans']


How can I pass any arguments to it?

Please, read my question carefully.

Derek

unread,
Jul 9, 2014, 8:10:20 AM7/9/14
to django...@googlegroups.com, teva...@googlemail.com
There is not really enough information about your use case (which is maybe why people seem to be misunderstanding your question).  

Its not really clear to me what you mean by "pass a parameter", as you have not given any indication where or how this parameter needs to be set.  

One way I have used to "subset" model lists in the admin is to use proxy models with model managers.  You could define a filter in a new model manager that automatically filters transactions, for example, from the last 7 days.  The proxy model (for example, LastWeekTransaction) can use this model manager to always show a list of the most recent Transactions.

If you actually want to run interactive queries, with users entering in their own date ranges, then you'll need to move beyond the admin app (e.g. by creating a new custome 'query' app), as its purpose is essentially for CRUD, and not for reporting.
Reply all
Reply to author
Forward
0 new messages