Django Admin ManyToManyField

时间:2011-11-07 22:57:35

标签: django django-models django-admin manytomanyfield django-orm

我做了一个模型(models.py):

class opetest(models.Model):
    name = models.CharField(max_length=200)
    author = models.ForeignKey(User, related_name='author')
    description = models.TextField(u'Test description', help_text = u'Some words about quiz')
    pub_date = models.DateTimeField('date published', blank=False)
    vacancies = models.ManyToManyField(Vacancy, blank=True)
    students = models.ManyToManyField(User, blank=True, related_name='opetests') #This field I want to edit on "User change page"
    estimate = models.IntegerField(default = 0, help_text = u'Estimate time in hours. \'0\' - unlimited')

然后我尝试添加内联块以允许在“更改用户”页面(admin.py)上分配opetest

class ProfileAdmin(UserAdmin):
    filter_horizontal = ('opetests',)

admin.site.unregister(User)
admin.site.register(User, ProfileAdmin)

我收到了一个错误:

'ProfileAdmin.filter_horizontal' refers to field 'opetests' that is missing from model 'User'.

我想在更改用户页面上显示 Groups 等操作数。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:44)

嗯,我认为你不想在这里内联。

您想要使用Django管理员的filter_horizo​​ntal:

https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.filter_horizontal

class ProfileAdmin(UserAdmin)
    filter_horizontal = ('opetest',)

这将为您提供您正在描述的小部件,用于在“用户更改”页面上添加/删除组。


好的,根据你的编辑,更新的答案 - 基本上,我们拥有的是一个UserProfile,链接到每个用户。

UserProfile包含与opetest的m2m关系 - 我们在admin中显示了filter_horizo​​ntal。最终结果是这样的:

Opetest with Filter horizontal

models.py

from django.db import models
from django.contrib.auth.models import User

class opetest(models.Model):
    name = models.CharField(max_length=200)
    author = models.ForeignKey(User, related_name='author')
    description = models.TextField(u'Test description', help_text = u'Some words about quiz')
    pub_date = models.DateTimeField('date published', blank=False)
    #vacancies = models.ManyToManyField(Vacancy, blank=True)
    students = models.ManyToManyField(User, blank=True, related_name='opetests') #This field I want to edit on "User change page"
    estimate = models.IntegerField(default = 0, help_text = u'Estimate time in hours. \'0\' - unlimited')

class UserProfile(models.Model):
    user = models.OneToOneField(User, unique=True)
    ope = models.ManyToManyField(opetest)
    test_flag = models.BooleanField()

admin.py

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from secondapp.models import UserProfile, opetest

admin.site.unregister(User)

class opetestAdmin(admin.ModelAdmin):
    pass

class UserProfileInline(admin.StackedInline):
    model = UserProfile
    filter_horizontal = ('ope',)

class CustomUserAdmin(UserAdmin):
    #filter_horizontal = ('user_permissions', 'groups', 'ope')
    save_on_top = True
    list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff', 'last_login')
    inlines = [UserProfileInline]

admin.site.register(User, CustomUserAdmin)
admin.site.register(opetest, opetestAdmin)

如果您有任何疑问或需要进一步了解,请与我们联系。