Create custom filter in popup of inlines. DjangoAdmin

  django, django-admin, python-3.x

Is it possible to create filter for one field in object creating in django admin.

For example, i need to add Video when creating Collection object, so i create

class VideoInline(admin.TabularInline):
    fields = ('video', )
    model = Collection.videos.through

But i need to filter videos in this popup by some fields, like created_at and so on.
Is it possible to do it?

Models:

class Collection(CreatedByModel, UpdatedByModel):

    name = models.CharField(
        max_length=150,
        verbose_name='Название подборки',
        unique=True,
    )
    videos = models.ManyToManyField(
        Video,
        through=CollectionVideoLink,
        verbose_name='Видео подборки',
        related_name='collection',
    )


class Video(  # noqa: WPS215
    SoftDeleteModelMixin,
    ReversionModelMixin,
    DirtyFieldsMixin,
    CreatedByModel,
):
    uuid = models.UUIDField(
        default=uuid.uuid4,
        editable=False,
        unique=True,
    )


class CollectionVideoLink(
    CreatedByModel,
    UpdatedByModel,
):

    video = models.ForeignKey(
        Video,
        on_delete=models.deletion.CASCADE,
        verbose_name='Video',
    )
    collection = models.ForeignKey(
        'Collection',
        on_delete=models.deletion.CASCADE,
        verbose_name='Collection',
    )

Source: Python-3x Questions

LEAVE A COMMENT