我需要django管理员中的多对多帮助。 保存在团队模型中的我的值未显示在我的成员部分中。下拉列表为空,在我的团队模型管理员中,数据仅显示在过滤器部分中,但不显示在主列表中。 这是我的模特:
DEPARTMENT = (
('C', 'Children'),
('C', 'Choir'),
('C', 'Cleaning'),
)
class Team(models.Model):
name = models.CharField(choices=DEPARTMENT, max_length=10)
member = models.ManyToManyField('Member', related_name='team')
class Meta:
verbose_name = 'Team'
def __str__(self):
return self.name
class Member(models.Model):
surname = models.CharField(max_length=20)
names = models.CharField(max_length=50)
gender = models.CharField(choices=GENDER, max_length=1)
dob = models.DateField()
contact_number = models.CharField(max_length=14, help_text="+27740000000")
email = models.EmailField(help_text='your email address')
address = models.CharField(max_length=50, help_text="you street, number, complex name only")
suburb = models.CharField(max_length=20, help_text="your suburb")
area_code = models.IntegerField(help_text="0000")
province = models.CharField(max_length=1, choices=PROVINCE)
occupation = models.CharField(max_length=30, help_text="Your daily occupation")
registered_date = models.DateField(default=timezone.now)
def __str__(self):
return self.surname
并在管理员中:
class TeamInline(admin.TabularInline):
model = Team.member.through
extra = 1
@admin.register(Member)
class MemberAdmin(admin.ModelAdmin):
list_display = ('surname', 'names', 'email', 'contact_number', 'suburb')
fieldsets = (
('Member personal details', {
'classes': ('grp-collapse grp-open',),
'fields': ('surname', 'names', 'gender', 'dob', 'contact_number', 'email',
'address', 'suburb', 'area_code', 'province', 'occupation', 'registered_date',
),
}),
)
list_filter = ['surname', 'email', 'suburb']
inlines = [MaritalInline, TeamInline, MembershipInline,]
@admin.register(Team)
class TeamAdmin(admin.ModelAdmin):
list_display = ('name',)
fieldsets = (
('Team details', {
'fields': ('name',),
}),
)
list_filter = ['name', ]
inlines = [TeamInline, ]
这是我的截图:
答案 0 :(得分:0)
内联适用于相关关系,而不是多对多关系。您必须使用表单字段。默认值为<select multiple>
,这不是很吸引人,但你可以使select2更加性感(参见django-autocomplete-light以简化select2与Django的集成)或create a custom field或{ {3}}