Adding inlines to existing User model changes User admin pages

I have Domain model with a many-to-many user field to the existing User model. I want a Domain select field when I Add or Change a User on these User Admin pages. I used admin.TabularInline to achive this. The attached images show how changed the Add page after add inlines to the code. The minimal fieldset extended to a full fieldset what Django uses when someone edits an existing user, and the fieldsets completely mixed compared to the original page without inlines. Original Add User page The changed Add User page

This is the actual code I tried, and the image above shows the working inlines with the Domain field, but I don't want to change the rest of the page. I'm satisfied with the existing Django User model and templates, I don't want a Custom User model and templates either, if I can avoid that.

# model.py

from django.db import models
from django.contrib.auth.models import User
from django.utils.translation import gettext as _

class Domain(models.Model):
    domain = models.CharField(_('Domain name'), max_length=100)
    master_dns_server = models.CharField(_('Master DNS server'), max_length=100, blank=True, default='')
    updatekey_secret = models.CharField(_('Key'), max_length=255, blank=True, default='')
    updatekey_name = models.CharField(_('Key name'), max_length=50, blank=True, default='')
    updatekey_algorythm = models.CharField(_('Key algorythm'), max_length=50, blank=True, default='')
    last_transfer = models.DateTimeField(_('Last transfer'), blank=True, null=True, editable=False)
    user = models.ManyToManyField(User, verbose_name=_("Domain user"), through='Domain_User')

    class Meta:
        ordering = ['domain']

    def __str__(self):
        return self.domain

class Domain_User(models.Model):
    domain = models.ForeignKey(Domain, on_delete=models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.CASCADE)

    class Meta:
        verbose_name = 'Domain user'

# admin.py

from django.contrib import admin
from .models import Domain
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth import get_user_model

class UserInline(admin.TabularInline):
    model = Domain.user.through
    verbose_name = 'Domain'
    extra = 0
    raw_id_fields = ['domain']

User = get_user_model()
admin.site.unregister(User)

class UserAdmin(admin.ModelAdmin):
    list_display = ['username', 'get_domains']
    inlines = [UserInline]

    def get_queryset(self, request):
        qs = super().get_queryset(request)
        return qs.prefetch_related('domain_set')

    @admin.display(description='domains')
    def get_domains(self, obj):
        return ", ".join([d.domain for d in obj.domain_set.all()])

admin.site.register(User, UserAdmin)

How can I use inlines in UserAdmin keeping the page's original content too?

Вернуться на верх