Django admin - 如果编辑对象则删除字段

发布于 2024-09-02 06:51:37 字数 398 浏览 4 评论 0原文

我有一个可以通过 Django 管理区域访问的模型,如下所示:

# model
class Foo(models.Model):
    field_a = models.CharField(max_length=100)
    field_b = models.CharField(max_length=100)

# admin.py
class FooAdmin(admin.ModelAdmin):
    pass

假设我想在用户添加对象时显示 field_a 和 field_b,但如果用户正在编辑对象则仅显示 field_a。有没有一种简单的方法可以做到这一点,也许使用 fields 属性?

如果到了这个地步,我可以破解一个 JavaScript 解决方案,但这样做感觉根本不对!

I have a model which is accessible through the Django admin area, something like the following:

# model
class Foo(models.Model):
    field_a = models.CharField(max_length=100)
    field_b = models.CharField(max_length=100)

# admin.py
class FooAdmin(admin.ModelAdmin):
    pass

Let's say that I want to show field_a and field_b if the user is adding an object, but only field_a if the user is editing an object. Is there a simple way to do this, perhaps using the fields attribute?

If if comes to it, I could hack a JavaScript solution, but it doesn't feel right to do that at all!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吻安 2024-09-09 06:51:38

您可以为管理员创建一个自定义 ModelForm ,以将该字段放入 __init__

class FooForm(forms.ModelForm):
    class Meta(object):
        model = Foo

    def __init__(self, *args, **kwargs):
        super(FooForm, self).__init__(*args, **kwargs)
        if self.instance and self.instance.pk:
            # Since the pk is set this is not a new instance
            del self.fields['field_b']

class FooAdmin(admin.ModelAdmin):
    form = FooForm

编辑:从 John 的评论中获取有关使字段只读的提示,您可以将其设置为隐藏字段并覆盖 clean 以确保值不会更改。

class FooForm(forms.ModelForm):
    class Meta(object):
        model = Foo

    def __init__(self, *args, **kwargs):
        super(FooForm, self).__init__(*args, **kwargs)
        if self.instance and self.instance.pk:
            # Since the pk is set this is not a new instance
            self.fields['field_b'].widget = forms.HiddenInput()

    def clean_field_b(self):
        if self.instance and self.instance.pk:
            return self.instance.field_b
        else:
            return self.cleaned_data['field_b']  

You can create a custom ModelForm for the admin to drop the field in the __init__

class FooForm(forms.ModelForm):
    class Meta(object):
        model = Foo

    def __init__(self, *args, **kwargs):
        super(FooForm, self).__init__(*args, **kwargs)
        if self.instance and self.instance.pk:
            # Since the pk is set this is not a new instance
            del self.fields['field_b']

class FooAdmin(admin.ModelAdmin):
    form = FooForm

EDIT: Taking a hint from John's comment about making the field read-only, you could make this a hidden field and override the clean to ensure the value doesn't change.

class FooForm(forms.ModelForm):
    class Meta(object):
        model = Foo

    def __init__(self, *args, **kwargs):
        super(FooForm, self).__init__(*args, **kwargs)
        if self.instance and self.instance.pk:
            # Since the pk is set this is not a new instance
            self.fields['field_b'].widget = forms.HiddenInput()

    def clean_field_b(self):
        if self.instance and self.instance.pk:
            return self.instance.field_b
        else:
            return self.cleaned_data['field_b']  
渡你暖光 2024-09-09 06:51:38

您还可以执行以下

class FooAdmin(admin.ModelAdmin)
    def change_view(self, request, object_id, extra_context=None):       
        self.exclude = ('field_b', )
        return super(SubSectionAdmin, self).change_view(request, object_id, extra_context)

操作Django admin:排除更改字段仅表格

You can also do the following

class FooAdmin(admin.ModelAdmin)
    def change_view(self, request, object_id, extra_context=None):       
        self.exclude = ('field_b', )
        return super(SubSectionAdmin, self).change_view(request, object_id, extra_context)

Taken from here Django admin: exclude field on change form only

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文