问题描述
我有一个模型, Foo
。它具有几个数据库属性,以及几个基于组合因素计算出的属性。我想将这些计算出的属性呈现给用户,就好像它们是数据库属性一样。 (支持因素将被更改以反映用户输入。)是否可以通过Django管理界面来做到这一点?
I have a model, Foo
. It has several database properties, and several properties that are calculated based on a combination of factors. I would like to present these calculated properties to the user as if they were database properties. (The backing factors would be changed to reflect user input.) Is there a way to do this with the Django admin interface?
推荐答案
我建议您为 Foo
(FooAdminForm)子类化一个模型表单,以添加自己的字段,而该字段不受数据库支持。您的自定义验证可以驻留在ModelForm的 clean _ *
方法中。
I would suggest you subclass a modelform for Foo
(FooAdminForm) to add your own fields not backed by the database. Your custom validation can reside in the clean_*
methods of ModelForm.
在 save_model内部
方法获得请求, FooAdmin
的 Foo
的实例以及表单数据,因此,您可以在保存实例之前/之后对数据进行所有处理。
Inside the save_model
method of FooAdmin
you get the request, an instance of Foo
and the form data, so you could do all processing of the data before/after saving the instance.
这里是一个示例的示例,该模型具有在django admin中注册的自定义表单:
Here is an example for a model with a custom form registered with django admin:
from django import forms
from django.db import models
from django.contrib import admin
class Foo(models.Model):
name = models.CharField(max_length=30)
class FooAdminForm(forms.ModelForm):
# custom field not backed by database
calculated = forms.IntegerField()
class Meta:
model = Foo
class FooAdmin(admin.ModelAdmin):
# use the custom form instead of a generic modelform
form = FooAdminForm
# your own processing
def save_model(self, request, obj, form, change):
# for example:
obj.name = 'Foo #%d' % form.cleaned_data['calculated']
obj.save()
admin.site.register(Foo, FooAdmin)
根据实例数据为自定义字段提供初始值
(我不确定这是否是最好的解决方案,但它是否应该工作。)
(I'm not sure if this is the best solution, but it should work.)
为数据库中现有模型实例的模型窗体构造时,它将通过此实例。因此,在FooAdminForm的 __ init __
中,可以根据实例数据更改字段属性。
When a modelform for a existing model instance in the database is constructed, it gets passed this instance. So in FooAdminForm's __init__
one can change the fields attributes based on instance data.
def __init__(self, *args, **kwargs):
# only change attributes if an instance is passed
instance = kwargs.get('instance')
if instance:
self.base_fields['calculated'].initial = (instance.bar == 42)
forms.ModelForm.__init__(self, *args, **kwargs)
这篇关于Django:伪造管理界面中的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!