django-nonrelated-inlines

raw JSON →
0.2 verified Mon Apr 27 auth: no python maintenance

Django admin inlines for unrelated models – allows using inline formsets for models without a direct foreign key relationship. Version 0.2, released to PyPI in 2021 (last release). Requires Django >= 3.0 and Python >= 3.6. Currently in maintenance mode with no recent updates.

pip install django-nonrelated-inlines
error django.core.exceptions.ImproperlyConfigured: Inline nonrelated_inlines.admin.NonRelatedTabularInline expects a ForeignKey but no related field found.
cause Django admin expects a foreign key relation; the library patches this but may fail if not set up correctly.
fix
Ensure you have imported NonRelatedTabularInline (or NonRelatedStackedInline) from nonrelated_inlines.admin and that your model does not have a direct FK; the inline is intended for unrelated models.
error 'NonRelatedTabularInline' object has no attribute 'fk_name'
cause Attempting to set fk_name on the inline class, which is not supported.
fix
Remove fk_name from the inline definition; use get_queryset to filter related objects.
gotcha You must override `get_queryset` manually; the inline does not automatically filter based on any relationship. Omitting this will show all objects in the inline.
fix Always provide a custom `get_queryset` method that filters by the parent instance.
gotcha The `fk_name` attribute is not used; it expects a foreign key but this library works with unrelated models. Setting `fk_name` will not work as expected.
fix Do not set `fk_name` on the inline class. Use `get_queryset` for filtering.
deprecated The library has not been updated since 2021 and may not be compatible with newer Django versions (4.x, 5.x). Some admin internals have changed.
fix Test with your Django version; consider forking or using alternative approaches (e.g., custom admin views).

Define a non-related inline by providing a custom get_queryset method that filters based on the parent object.

from django.contrib import admin
from nonrelated_inlines.admin import NonRelatedTabularInline
from .models import Author, Book

class BookInline(NonRelatedTabularInline):
    model = Book
    fields = ['title', 'published_date']
    # Define how the inline relates to the parent via a custom queryset
    def get_queryset(self, request, obj):
        if obj:
            return self.model.objects.filter(author=obj)
        return self.model.objects.none()

@admin.register(Author)
class AuthorAdmin(admin.ModelAdmin):
    inlines = [BookInline]