允许在 Django Admin 中对 editable=False 字段进行编辑 [英] Allowing Edit to editable=False Fields in Django Admin
问题描述
DRF 将使用 字段上的 editable=False
将序列化程序默认为只读.这是我利用的一个非常有用/安全的默认设置(即我不会忘记将序列化程序设置为只读).话虽如此,一旦我设置了 editable=False
有没有办法强制 Django 管理员允许编辑这些字段之一?
DRF will use the editable=False
on a field to default the Serializer to read-only. This is a very helpful / safe default that I take advantage of (ie I won't forget to set the Serializer to read-only). That being said once I have set editable=False
is there any way to then force the Django admin to allow editing one of those fields?
大概管理员是超级用户,我确实希望他能够更改字段值,但为了安全起见,我希望默认的 Serializer
逻辑是只读的.
Presumably the admin is a super user and I do want him to be able to change the fields value but fore safety I want the default Serializer
logic to be read only.
更新
当我创建对象时,我实际上不需要像设置它"那样编辑字段.
I don't actually need to be able to edit the field as much as "set-it" when I create the object.
推荐答案
你的做法是错误的.
您的模型应该是您正在建模的事物的最纯粹的实现.如果某个模型的某些内容是固定的(例如创建日期),则不应在模型中对其进行编辑,如果是可变的,则在模型中保留为可编辑.
Your models should be the most pure implementation of the things you are modelling. If something about a model is fixed (for example a creation date) it shouldn't be editable in the model, if its mutable, then leave as editable in the model.
否则,将来您(或其他人)可能会想知道为什么设置为 editable=False
的字段是如何更改的.尤其是如文档所述:
Otherwise, in the future you (or someone else) might be stuck wondering why a field which is set to editable=False
is some how being changed. Especially as the documentation states:
如果为 False,该字段将不会显示在管理员或任何其他 ModelForm 中.在模型验证期间也会跳过它们.
If False, the field will not be displayed in the admin or any other ModelForm. They are also skipped during model validation.
如果您有一个不应在其中进行编辑的视图(例如在 API 中),则在那里覆盖它.
If you have one view in which it shouldn't be editable (such as in the API), then override it there.
如果你有一个模型的多个序列化器,那么用 read_only_fields
设置然后子类化.例如:
If you have multiple serilaizers for a model, instead make an abstract serializer with a read_only_fields
set and then subclass that. For example:
class AbstractFooSerializer(serializers.ModelSerializer):
class Meta:
model = Foo
read_only_fields = ('bar',)
class MainFooSerializer(AbstractFooSerializer):
pass
class DifferentFooSerializer(AbstractFooSerializer):
pass
<小时>
如果您真的很想使用 editable=False
,但允许在管理站点中编辑项目仅在创建时,那么您将面临一场艰难的战斗.
If you really, really want to use editable=False
, but allow the item to be edited in the Admin site only on creation you have an up hill battle.
可能最好的方法是重新实现您用于管理员的 AdminForm
Probably the best approach would be to reimplement the AdminForm
you are using for the Admin
所以代替:
class FooAdmin(admin.ModelAdmin):
使用:
class FooAdmin(admin.ModelAdmin):
form = MySpecialForm
然后声明表单:
class MySpecialForm(forms.Model):
def __init__(self, *args, **kwargs):
self.is_new = False
if kwargs.get('instance',None) is None:
# There is no instance, thus its a new item
self.is_new = True
self.fields['one_time_field'] = forms.CharField() # Or what have you.
super(MySpecialForm, self).__init__(*args, **kwargs)
def save(self, commit=True):
instance = super(MySpecialForm, self).save(commit)
if self.is_new:
instance.your_one_time_only_field = self.one_time_field
instance.save()
return instance
注意:您需要手动添加一个字段并保存您要为其执行此操作的每个 readonly
字段.这可能是也可能不是 100% 的功能.
Note: you will need to manually add a field and save each readonly
field that you want to do this for. This may or may not be 100% functional.
这篇关于允许在 Django Admin 中对 editable=False 字段进行编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!