在Django Admin中允许Edit to editable = False字段 [英] Allowing Edit to editable=False Fields in Django Admin

查看:405
本文介绍了在Django Admin中允许Edit to editable = False字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DRF将使用 editable = False 在字段上将序列化程序默认为只读。这是一个非常有用/安全的默认设置,我可以利用它(即,我不会忘记将Serializer设置为只读)。话虽这么说,一旦我设置了 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.

更新

创建对象时,实际上我不需要编辑 set-it字段。

I don't actually need to be able to edit the field as much as "set-it" when I create the object.

推荐答案

您将以错误的方式进行操作。

You are going about this the wrong way.

您的模型应该是所建模事物的最纯粹的实现。如果某个模型的某些问题是固定的(例如创建日期),则该模型不应在模型中进行编辑,如果其可变,则应在模型中保留为可编辑状态。

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中允许Edit to editable = False字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆