hebius
hebius

Reputation: 133

Django - filtering based on filed from other model

I am working on some Django app and I met with a problem that I don't know how to solve.

I have to models:

class A(models.Model):
    STATE_NEW = 1
    STATE_DELIVERED = 2
    STATE_LOST = 3

    STATE_CHOICES (
        (STATE_NEW, 'New order'),
        (STATE_DELIVERED, 'Delivered'),
        (STATE_LOST, 'Lost'),
    )
order_id = models.IntegerField()
status = models.IntegerField(u'Status', default=NEW_STATE, choices=STATE_CHOICES)

class B(models.Model):
    # some_fields
    order = models.ForeignKey(A)

I have some fields in model B. What I want to achieve is to add filter to admin site for model B that allows filtering on "status" (field from model A). Is it possible to achieve it without defining new field in model B?

Nothing comes to my mind because I don't want to keep "status" field with all possible STATE_CHOICES in model B (because "status" is not a field of model B).

Could you please help me? :)

Upvotes: 0

Views: 52

Answers (1)

Dima  Kudosh
Dima Kudosh

Reputation: 7366

You can do this using list_filter

list_filter = ('order__status', )

from docs:

Field names in list_filter can also span relations using the __ lookup

Upvotes: 3

Related Questions