[Python] Filter inline field in Django Admin

Discussão em 'Python' iniciado por Stack, Julho 17, 2025.

  1. Stack

    Stack Membro Participativo

    I have the following models:

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

    class Article(models.Model):
    provider = models.ManyToManyField(Provider)
    ...

    class Invoice(models.Model):
    provider = models.ForeignKey(Provider)
    ...

    class ArticleInvoice(models.Model):
    article = models.ForeignKey(Article)
    invoice = models.ForeignKey(Invoice)
    ... #qty, status, etc


    and my admin looks like:

    class ArticleInvoiceInline(admin.TabularInline):
    model = ArticleInvoice

    class InvoiceAdmin(models.Model):
    inlines = [ArticleInvoiceInline]

    admin.site.register(Invoice, InvoiceAdmin)


    The problem:

    When I create an Invoice I have to chose a Provider, then add some ArticleInvoices, the ArticleInvoice have an Article which has a Provider, when I choose the Article for the ArticleInvoice it shows all the articles.

    I would like to apply a filter so I can only create ArticleInvoices in the InvoiceAdmin with Articles that have the same Provider as the Invoice

    Continue reading...

Compartilhe esta Página