在我的django ModelAdmin中,我想基于当前用户的组过滤外键选择,我希望以添加和更改形式实现它。我通过使用以下代码在添加视图中实现了它。
add_view
def add_view(self, request, form_url = '', extra_context = None):
service_sector = common.getServiceSector(request.user)
ModelForm = self.get_form(request)
if request.POST:
form = ModelForm(request.POST, request.FILES)
else:
form = ModelForm()
if service_sector:
qs = form['member'].field.queryset
qs = qs.filter(service_sector = service_sector)
form['member'].field.queryset = qs
adminForm = helpers.AdminForm(form, list(self.get_fieldsets(request)),
self.prepopulated_fields, self.get_readonly_fields(request),
model_admin=self)
context = {
'adminform': adminForm,
'is_popup': "_popup" in request.REQUEST,
'show_delete': False,
'root_path': self.admin_site.root_path,
}
context.update(extra_context or {})
return self.render_change_form(request, context, form_url=form_url, add=True)
这里工作正常。在此模型中,我有一个唯一的字段电子邮件,这会导致change_view出现问题。我在下面给出了我的change_view代码。
change_view
def change_view(self, request, object_id, form_url = '', extra_context = None):
qs = self.model._default_manager.get_query_set()
service_sector = common.getServiceSector(request.user)
bene_object = Beneficiary.objects.get(pk=object_id)
ModelForm = self.get_form(request, bene_object)
if request.POST:
form = ModelForm(request.POST, request.FILES)
else:
form = ModelForm(instance=bene_object)
if service_sector:
qs = form['member'].field.queryset
qs = qs.filter(service_sector = service_sector)
form['member'].field.queryset = qs
adminForm = helpers.AdminForm(form, list(self.get_fieldsets(request)),
self.prepopulated_fields, self.get_readonly_fields(request),
model_admin=self)
context = {
'adminform': adminForm,
'is_popup': "_popup" in request.REQUEST,
'show_delete': False,
'root_path': self.admin_site.root_path,
}
context.update(extra_context or {})
return self.render_change_form(request, context, form_url = form_url, change = True)
即使我在change_view返回中给出change = True
,它也会尝试将对象保存为新对象。因此,我收到错误Email already exists
,因为电子邮件字段是唯一值字段。或者是否可以使用正常的change_view返回如下:
return super(ModelAdmin, self).change_view(request, form_url, extra_context)
如果是这样,我该如何过滤外键选择。或者如何使用render_change_form
来实现这一目标?提前谢谢。
答案 0 :(得分:4)
我已经解决了这个问题并在这里发布了可能有助于其他人的答案。问题here帮助我解决了这个问题。使用以下代码,我已经基于用户的组完成了foreignkey过滤器,在django admin中添加和更改表单,其中包含内联表单集。我给出了以下代码:
def render_change_form(self, request, context, *args, **kwargs):
model = self.model
opts = model._meta
formsets = []
#Change view
if 'change' in kwargs.keys():
object_id = kwargs['obj'].id #To get object id
object = Modelobjects.get(pk=object_id)
ModelForm = self.get_form(request, object)
qs = self.model._default_manager.get_query_set()
if request.POST:
form = ModelForm(request.POST, request.FILES)
else:
form = ModelForm(instance = object)
service_sector = common.getServiceSector(request.user)
if service_sector:
#To filter foreignkey
qs = form['member'].field.queryset
qs = qs.filter(service_sector = service_sector)
form['member'].field.queryset = qs
adminForm = helpers.AdminForm(form, list(self.get_fieldsets(request)),
self.prepopulated_fields, self.get_readonly_fields(request),
model_admin=self)
media = self.media + adminForm.media
#To get inline formsets used
prefixes = {}
for FormSet, inline in zip(self.get_formsets(request),
self.inline_instances):
prefix = FormSet.get_default_prefix()
prefixes[prefix] = prefixes.get(prefix, 0) + 1
if prefixes[prefix] != 1:
prefix = "%s-%s" % (prefix, prefixes[prefix])
formset = FormSet(instance=object, prefix=prefix,
queryset=inline.queryset(request))
formsets.append(formset)
inline_admin_formsets = []
for inline, formset in zip(self.inline_instances, formsets):
fieldsets = list(inline.get_fieldsets(request))
readonly = list(inline.get_readonly_fields(request))
inline_admin_formset = helpers.InlineAdminFormSet(inline, formset,
fieldsets, readonly, model_admin=self)
inline_admin_formsets.append(inline_admin_formset)
media = media + inline_admin_formset.media
else:
#Add view
ModelForm = self.get_form(request)
qs = self.model._default_manager.get_query_set()
if request.POST:
form = ModelForm(request.POST, request.FILES)
else:
form = ModelForm()
service_sector = common.getServiceSector(request.user)
if service_sector:
qs = form['member'].field.queryset
qs = qs.filter(service_sector = service_sector)
form['member'].field.queryset = qs
adminForm = helpers.AdminForm(form, list(self.get_fieldsets(request)),
self.prepopulated_fields, self.get_readonly_fields(request),
model_admin=self)
media = self.media + adminForm.media
prefixes = {}
for FormSet, inline in zip(self.get_formsets(request),
self.inline_instances):
prefix = FormSet.get_default_prefix()
prefixes[prefix] = prefixes.get(prefix, 0) + 1
if prefixes[prefix] != 1:
prefix = "%s-%s" % (prefix, prefixes[prefix])
formset = FormSet(instance=self.model(), prefix=prefix,
queryset=inline.queryset(request))
formsets.append(formset)
inline_admin_formsets = []
for inline, formset in zip(self.inline_instances, formsets):
fieldsets = list(inline.get_fieldsets(request))
readonly = list(inline.get_readonly_fields(request))
inline_admin_formset = helpers.InlineAdminFormSet(inline, formset,
fieldsets, readonly, model_admin=self)
inline_admin_formsets.append(inline_admin_formset)
media = media + inline_admin_formset.media
context = {
'adminform': adminForm,
'title': _('Add %s') % force_unicode(opts.verbose_name),
'is_popup': "_popup" in request.REQUEST,
'show_delete': False,
'media': mark_safe(media),
'inline_admin_formsets': inline_admin_formsets,
'root_path': self.admin_site.root_path,
}
return super(ModelAdmin, self).render_change_form(request, context, args, kwargs)
此处对象id是从kwargs['obj'].id
获得的,而foreignkey是通过
qs = form['member'].field.queryset
qs = qs.filter(service_sector = service_sector)
form['member'].field.queryset = qs
这里common.getServiceSector
是我用于外键过滤条件的自定义方法。您可以根据需要定义自己的方法。希望这对其他人有所帮助。感谢。