To display inlines which are not related (no foreign key pointing at the main model) to the model instance in changeform, you can use nonrelated inlines which are included in unfold.contrib.inlines
module. Make sure this module is included in INSTALLED_APPS
in settings.py.
from django.contrib.auth.models import User
from unfold.admin import ModelAdmin
from unfold.contrib.inlines.admin import NonrelatedTabularInline
from .models import OtherModel
class OtherNonrelatedInline(NonrelatedTabularInline): # NonrelatedStackedInline is available as well
model = OtherModel
fields = ["field1", "field2"] # Ignore property to display all fields
def get_form_queryset(self, obj):
"""
Gets all nonrelated objects needed for inlines. Method must be implemented.
"""
return self.model.objects.all()
def save_new_instance(self, parent, instance):
"""
Extra save method which can for example update inline instances based on current
main model object. Method must be implemented.
"""
pass
@admin.register(User)
class UserAdmin(ModelAdmin):
inlines = [OtherNonrelatedInline]
NOTE: credit for this functionality goes to django-nonrelated-inlines
Django admin theme built with Tailwind CSS to bring modern look and feel to your admin interface. Already contains several built-in features for smooth developer experience.
© 2023 - 2024 Created by unfoldadmin.com. All rights reserved.