我在models.py中有以下内容:
class Choices(models.Model):
description = models.CharField(max_length=300)
def __unicode__(self):
return self.description
class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
fullname = models.CharField(_('Full Name'), max_length=50)
birthdate = models.DateTimeField(_('Birthday'))
url = models.URLField(_('Website'), blank=True)
company = models.CharField(_('Company'), max_length=50)
CATEGORY_CHOICES = (
('M',_('Male')),
('F',_('Female')),
)
gender = models.CharField(_('Gender'), max_length=200, choices = CATEGORY_CHOICES)
picture = models.ImageField(_('Picture/Avatar'), upload_to='profile_photo', blank=True)
bio = models.TextField()
planguages = models.ManyToManyField(Choices)
现在,我希望能够在管理员中编辑用户个人资料,以及填写用户在创建个人资料时可以选择的选项。作为一个django newb我,我设法只选择编辑用户个人资料,但没有设法填充选择。无论如何,我可以做到,但我不知道如何兼得。
EDITED: 这是我目前的admin.py
class ChoicesAdmin(admin.ModelAdmin):
list_display = ['description']
class ProfilesAdmin(admin.ModelAdmin):
list_display = ['user']
admin.site.register(UserProfile, ProfilesAdmin)
答案 0 :(得分:1)
您要找的是filter_horizontal
class ChoicesAdmin(admin.ModelAdmin):
list_display = ['description']
class ProfilesAdmin(admin.ModelAdmin):
list_display = ['user']
filter_horizontal = ['planguages']
admin.site.register(UserProfile, ProfilesAdmin)
admin.site.register(Choices, ChoicesAdmin)
这将允许您向用户添加语言。
此外,您必须注册ChoicesAdmin类才能看到绿色+链接以添加选项。
答案 1 :(得分:-1)
在文档中查找内联。