仅在 Django admin inline 中对现有项目只读 [英] Readonly for existing items only in Django admin inline

查看:14
本文介绍了仅在 Django admin inline 中对现有项目只读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Django 管理员中有一个表格内联模型.我需要其中 1 个字段在创建后不可更改,但将其设置为只读(通过 readonly_fields)可以正常工作,但在单击添加另一个项目"而不是下拉菜单时将字段转换为标签.

I have a tabular inline model in the Django admin. I need 1 of the fields to not be changeable after it has been created, but setting it as readonly (via readonly_fields) which works fine, but turns the field into a label when clicking 'Add another item' instead of a dropdown.

有没有办法让字段保持只读,但仍允许使用正确的字段输入创建新项目?

Is there a way to keep a field readonly, but still allow new items to be created with the proper field input?

谢谢!

托马斯

通过自定义小部件设法弄清楚

Managed to figure it out by way of a custom widget

class ReadOnlySelectWidget(forms.Select):
    def render(self, name, value, attrs=None):
        if value:
            final_attrs = self.build_attrs(attrs, name=name)
            output = u'<input value="%s" type="hidden" %s />' % (value, flatatt(final_attrs))
            return mark_safe(output + str(self.choices.queryset.get(id=value)))
        else:
            return super(ReadOnlySelectWidget, self).render(name, value, attrs)

如果有值,它只会将其变为隐藏,并非在所有情况下都有效(仅适用于 1 个只读字段).

It just turns it into a hidden if there is a value, won't work in every situation (only really works with 1 read only field).

推荐答案

遇到同样的问题,我遇到了这个问题:

Having the same problem, I came across this fix:

创建两个内联对象,一个没有更改权限,另一个所有字段都是只读的.将两者都包含在模型管理中.

Create two inline objects, one with no change permission, and the other with all the fields read-only. Include both in the model admin.

class SubscriptionInline(admin.TabularInline):
    model = Subscription
    extra = 0
    readonly_fields = ['subscription', 'usedPtsStr', 'isActive', 'activationDate', 'purchaseDate']

    def has_add_permission(self, request):
        return False

class AddSupscriptionInline(admin.TabularInline):
    model = Subscription
    extra = 0
    fields = ['subscription', 'usedPoints', 'isActive', 'activationDate', 'purchaseDate']

    def has_change_permission(self, request, obj=None):
        return False

    # For Django Version > 2.1 there is a "view permission" that needs to be disabled too (https://docs.djangoproject.com/en/2.2/releases/2.1/#what-s-new-in-django-2-1)
    def has_view_permission(self, request, obj=None):
        return False

将它们包含在同一个模型管理员中:

Include them in the same model admin:

class UserAdmin(admin.ModelAdmin):
    inlines = [ AddSupscriptionInline, SubscriptionInline]

要添加新订阅,我在管理中使用 AddSubscriptionInline.保存后,新订阅将从该内联中消失,但现在确实以只读形式出现在 SubscriptionInline 中.

To add a new subscription I use the AddSubscriptionInline in the admin. Once it is saved, the new subscription disappears from that inline, but now does appear in the SubscriptionInline, as read only.

对于SubscriptionInline,重要的是要提及extra = 0,这样它就不会显示垃圾只读订阅.最好也隐藏 SubscriptionInline 的添加选项,只允许通过 AddSubscriptionInline 添加,方法是将 has_add_permission 设置为始终返回 错误.

For SubscriptionInline, it is important to mention extra = 0, so it won't show junk read-only subscriptions. It is better also to hide the add option for SubscriptionInline, to allow adding only via AddSubscriptionInline, by setting the has_add_permission to always return False.

一点也不完美,但它对我来说是最好的选择,因为我必须提供在用户管理页面上添加订阅的功能,但添加后,只能通过内部应用逻辑进行更改.

Not perfect at all, but it's the best option for me, since I must provide the ability to add subscriptions on the user admin page, but after one is added, it should be changed only via the internal app logic.

这篇关于仅在 Django admin inline 中对现有项目只读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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