简体   繁体   中英

Django Admin Inlines and AbstractUser

I'm having issues with trying to have MtoM in my admin.py. I keep getting an error that says: <class 'demo.admin.CustomUserAdmin'>: (admin.E013) The value of 'fieldsets[2][1]["fields"]' cannot include the ManyToManyField 'groups', because that field manually specifies a relationship model.

#admin.py
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from app.forms import CustomUserCreationForm, CustomUserChangeForm
from app.models import User, GroupUser, RoleAssign

class GroupUserInline(admin.TabularInline):
    model = GroupUser

class CustomUserAdmin(UserAdmin):
    add_form = CustomUserCreationForm
    form = CustomUserChangeForm
    model = User
    inlines = [ GroupUserInline, ]

admin.site.register(User, CustomUserAdmin)

and these are my models:

#models.py
class Group(models.Model):
    name  = models.CharField(max_length=240)
    roles = models.ManyToManyField(Role, through='RoleAssign')

class User(AbstractUser):
    username   = models.CharField(max_length=240, blank=False, null=False, unique=True)
    password   = models.CharField(max_length=256, blank=False, null=False)
    email      = models.EmailField(max_length=256, blank=False, null=False, unique=True)
    S3         = models.TextField()
    groups     = models.ManyToManyField(Group, through='GroupUser')

class GroupUser(models.Model):
    user  = models.ForeignKey(settings.AUTH_USER_MODEL)
    group = models.ForeignKey(Group)

Does anyone have any details why my inline may not be working? Or should I be trying a different method?

Try use model = User.groups.through in GroupUserInline class

View more here , I think this can help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM