禁止 Django 自引用外键指向自身对象
Disallow Django self-referential foreign key to point to self object
我有一个常规的自引用外键:
idol = models.ForeignKey("self", on_delete=models.CASADE)
管理页面允许我选择相同的对象 ID。如何防止 django 管理表单显示它?
您应该能够将模型管理员的表单设置为包含修改的自定义表单
class ModelAdmin...
form = MyModelForm
我想表格应该是这样的
class MyModelForm(ModelForm)
class Meta:
model = MyModel
def __init__(self, *args, **kwargs):
super(MyModelForm, self).__init__(*args, **kwargs)
if self.instance.pk:
query = self.fields['my_field'].queryset
self.fields['my_field'].queryset = query.exclude(id=self.instance.id)
您可以在 ModelAdmin
class 的子 class 中覆盖 formfield_for_foreignkey
。
The formfield_for_foreignkey method on a ModelAdmin allows you to
override the default formfield for a foreign keys field.
父对象id可以保存在change_view
方法中:
class IdolAdmin(admin.ModelAdmin):
def change_view(self, request, object_id, form_url='', extra_context=None):
self.object_id = object_id
return super(IdolAdmin, self).change_view(
request, object_id, form_url, extra_context=extra_context,
)
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "idol":
kwargs['queryset'] = Idol.objects.exclude(pk=self.object_id)
return super(IdolAdmin, self).formfield_for_foreignkey(
db_field, request, **kwargs)
您可以扩展 render_change_form
.
class IdolAdmin(admin.ModelAdmin):
def render_change_form(self, request, context, *args, **kwargs):
queryset = context['adminform'].form.fields['idol'].queryset
original_id = context.get('original', None)
if(original_id):
context['adminform'].form.fields['idol'].queryset = queryset.exclude(id=original_id.id)
return super(IdolAdmin, self).render_change_form(request, context, args, kwargs)
我有一个常规的自引用外键:
idol = models.ForeignKey("self", on_delete=models.CASADE)
管理页面允许我选择相同的对象 ID。如何防止 django 管理表单显示它?
您应该能够将模型管理员的表单设置为包含修改的自定义表单
class ModelAdmin...
form = MyModelForm
我想表格应该是这样的
class MyModelForm(ModelForm)
class Meta:
model = MyModel
def __init__(self, *args, **kwargs):
super(MyModelForm, self).__init__(*args, **kwargs)
if self.instance.pk:
query = self.fields['my_field'].queryset
self.fields['my_field'].queryset = query.exclude(id=self.instance.id)
您可以在 ModelAdmin
class 的子 class 中覆盖 formfield_for_foreignkey
。
The formfield_for_foreignkey method on a ModelAdmin allows you to override the default formfield for a foreign keys field.
父对象id可以保存在change_view
方法中:
class IdolAdmin(admin.ModelAdmin):
def change_view(self, request, object_id, form_url='', extra_context=None):
self.object_id = object_id
return super(IdolAdmin, self).change_view(
request, object_id, form_url, extra_context=extra_context,
)
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "idol":
kwargs['queryset'] = Idol.objects.exclude(pk=self.object_id)
return super(IdolAdmin, self).formfield_for_foreignkey(
db_field, request, **kwargs)
您可以扩展 render_change_form
.
class IdolAdmin(admin.ModelAdmin):
def render_change_form(self, request, context, *args, **kwargs):
queryset = context['adminform'].form.fields['idol'].queryset
original_id = context.get('original', None)
if(original_id):
context['adminform'].form.fields['idol'].queryset = queryset.exclude(id=original_id.id)
return super(IdolAdmin, self).render_change_form(request, context, args, kwargs)