我有一个与另一个人有很多关系的模特。我按照https://gist.github.com/jdklub/9261959上的示例操作,以便从changelist视图中编辑manytomany字段。但是,当我进行更改时,实际上只保存了对manytomany字段的更改。
admin.py
class HostChangeList(ChangeList):
def __init__(self, request, model, list_display, list_display_links,
list_filter, date_hierarchy, search_fields, list_select_related,
list_per_page, list_max_show_all, list_editable, model_admin):
super(HostChangeList, self).__init__(request, model, list_display, list_display_links,
list_filter, date_hierarchy, search_fields, list_select_related,
list_per_page, list_max_show_all, list_editable, model_admin)
self.list_display = ('action_checkbox', 'name', 'ip', 'fagroup', 'description', 'customer_facing', 'monitored', 'puppet', 'sso')
self.list_editable = ['fagroup', 'description', 'customer_facing', 'monitored', 'puppet', 'sso']
self.list_display_links = ['name']
class HostForm(forms.ModelForm):
fagroup = forms.ModelMultipleChoiceField(queryset=FAgroup.objects.all(),widget=forms.SelectMultiple(attrs={'size': '10'}),required=False)
description = forms.CharField(required=False)
customer_facing = forms.NullBooleanField()
monitored = forms.NullBooleanField()
puppet = forms.NullBooleanField()
sso = forms.NullBooleanField()
class HostAdmin(admin.ModelAdmin):
def get_changelist(self, request, **kwargs):
return HostChangeList
def get_changelist_form(self, request, **kwargs):
return HostForm
models.py
class Host(models.Model):
name = models.CharField(max_length=200)
ip = models.GenericIPAddressField(default='0.0.0.0')
fagroup = models.ManyToManyField(FAgroup)
description = models.TextField(default=None, blank=True, null=True)
customer_facing = models.NullBooleanField(default=False, null=False)
monitored = models.NullBooleanField(default=False, null=True)
puppet = models.NullBooleanField(default=False, null=True)
sso = models.NullBooleanField(default=False, null=True)
def __str__(self):
return self.name
def display_fagroups(self):
return ', '.join([ group.name for group in self.fagroup.all() ])
class FAgroup(models.Model):
name = models.CharField(max_length=200)
def __str__(self):
return self.name
我甚至尝试从ModelForm和ChangeList中删除对manytomany字段的所有引用,但没有字段保存。它没有给出任何错误,并且确实给出了保存记录的消息,但没有任何更改持续存在。
答案 0 :(得分:1)
我不确定这是否是解决此问题的正确方法,但我可以通过覆盖HostAdmin类的内置保存功能来保存它。希望这有助于其他人查看类似的问题。
class HostAdmin(admin.ModelAdmin):
search_fields = ('name', 'ip', 'fagroup__name')
list_filter = ('fagroup', 'customer_facing', 'monitored', 'puppet', 'sso')
actions_on_bottom = True
def get_changelist(self, request, **kwargs):
return HostChangeList
def get_changelist_form(self, request, **kwargs):
return HostForm
def save_model(self, request, obj, form, change):
if form.cleaned_data['fagroup'] != obj.fagroup:
obj.fagroup = form.cleaned_data['fagroup']
if form.cleaned_data['description'] != obj.description:
obj.description = form.cleaned_data['description']
if form.cleaned_data['customer_facing'] != obj.customer_facing:
obj.customer_facing = form.cleaned_data['customer_facing']
if form.cleaned_data['monitored'] != obj.monitored:
obj.monitored = form.cleaned_data['monitored']
if form.cleaned_data['puppet'] != obj.puppet:
obj.puppet = form.cleaned_data['puppet']
if form.cleaned_data['sso'] != obj.sso:
obj.sso = form.cleaned_data['sso']
obj.save()