问题描述
我制作了一个模型(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关于测验的话)
pub_date = models.DateTimeField('date published',blank = False)
vacancies = models.ManyToManyField(空缺,空白= True)
students = models.ManyToManyField User,blank = True,related_name ='opetests')#这个字段我想在用户更改页面上编辑
estimate = models.IntegerField(default = 0,help_text = u'Estimate time in hours。\ '0\' - 无限制')
然后我尝试添加内联块以允许赋值code> opetest 更改用户页面(admin.py):
class ProfileAdmin (UserAdmin):
filter_horizontal =('opetests')
admin.site.unregis ter(User)
admin.site.register(User,ProfileAdmin)
一个错误:
'ProfileAdmin.filter_horizontal'是指从模型User中缺少的字段opetests。
我想在上显示组 / em>页面。如何实现?
嗯,我不认为你想在这里内联。
你想使用Django管理员的filter_horizontal:
class ProfileAdmin(UserAdmin)
filter_horizontal =('opetest',)
这将为您提供您正在描述的小部件,用于在用户上添加/删除组更改页面。
好的,根据你的编辑,更新的答案 - 基本上,我们有一个UserProfile,链接到每个用户。
UserProfile包含与opetest的m2m关系 - 我们在admin中显示一个filter_horizontal。最终结果是这样的:
models.py
$ $ $ $ $ $ $ $ $ $ $
$ b class opetest(models.Model)
name = model .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(空缺,空白= True)
students = models.ManyToManyField(用户,空白= True,related_name ='opetests')#此字段我想在用户更改页面上编辑
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
<$来自django.contrib的p $ p>
从django.contrib.auth.admin导入admin
从django.contrib.auth.models导入UserAdmin
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(Us呃,CustomUserAdmin)
admin.site.register(opetest,opetestAdmin)
让我知道如果您有任何问题,或需要任何进一步的。
I've made a model (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')
then I try to add inline block to allow assign opetest
on 'change user' page (admin.py):
class ProfileAdmin(UserAdmin):
filter_horizontal = ('opetests',)
admin.site.unregister(User)
admin.site.register(User, ProfileAdmin)
And I got an error:
'ProfileAdmin.filter_horizontal' refers to field 'opetests' that is missing from model 'User'.
I want to show opetests like Groups on change user page. How can I achieve that?
Hmm, I don't think you want inlines here.
You want to be using the Django admin's filter_horizontal:
class ProfileAdmin(UserAdmin)
filter_horizontal = ('opetest',)
That will give you the widget that you're describing, used to add/remove Groups on the User Change page.
Ok, based on your edits, updated answer - basically, what we have is a UserProfile, linked to each user.
The UserProfile contains a m2m relationship to opetest - which we show in the admin with a filter_horizontal. End result is something like this:
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)
Let me know if you have any questions, or need anything further.
这篇关于Django管理ManyToManyField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!