只读表单字段格式

发布于 2024-12-21 15:46:18 字数 343 浏览 1 评论 0原文

我想在 ModelAdmin 表单中将字段显示为只读,因此我将其添加到 readonly_fields 属性中。

但是,由于该字段包含以整数形式存储的货币,因此我想对其应用一些很好的格式。我为我的 ModelAdmin 创建了一个自定义 ModelForm,尝试在重写的 __init__ 方法中应用格式。

问题是,我找不到这个值。该字段不存在于 self.fields 属性中。

有谁知道 readonly_fields 的值保存在哪里,或者是否有更好/不同的方法?

I want to display a field as read only in a ModelAdmin form, so I added it to the readonly_fields attribute.

However, since the field contains a currency, stored as an integer, I want to apply some nice formatting it. I've created a custom ModelForm for my ModelAdmin, trying to apply the formatting in the overridden __init__ method.

The problem is, I cannot find the value. The field is not present in the self.fields attribute.

Does anyone know where the values for the readonly_fields are kept, or is there a better/different approach?

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

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

发布评论

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

评论(3

_畞蕅 2024-12-28 15:46:18

适用于所有类型表单的另一种方法是创建一个小部件来表示只读字段。这是我写的供我自己使用的一篇。您可以更改 %s 以满足您自己的要求。

from django import forms
from django.utils.safestring import mark_safe
from django.utils.encoding import force_unicode


class ReadOnlyWidget(forms.TextInput):
    def render(self, name, value, attrs=None):
         if value is None:
             value = ''
         final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
         if value != '':
             # Only add the 'value' attribute if a value is non-empty.
             final_attrs['value'] = force_unicode(self._format_value(value))
         return mark_safe(u'<span%s />%s</span>' % (flatatt(final_attrs),value))

添加后,只需执行以下操作:

class MyAdmin(admin.ModelAdmin):
    foo = models.TextField(widget=ReadOnlyWidget(attrs={'class':'read-only'}
                           initial="$50")

然后在 CSS 中,为 read-only 类进行一些样式设置,或者您可以相应地调整属性。

An alternate approach, which works for all types of forms is to create a widget to represent a read only field. Here is one that I wrote for my own use. You can change the <span %s>%s</span> to suit your own requirements.

from django import forms
from django.utils.safestring import mark_safe
from django.utils.encoding import force_unicode


class ReadOnlyWidget(forms.TextInput):
    def render(self, name, value, attrs=None):
         if value is None:
             value = ''
         final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
         if value != '':
             # Only add the 'value' attribute if a value is non-empty.
             final_attrs['value'] = force_unicode(self._format_value(value))
         return mark_safe(u'<span%s />%s</span>' % (flatatt(final_attrs),value))

Once you have that added, simply do this:

class MyAdmin(admin.ModelAdmin):
    foo = models.TextField(widget=ReadOnlyWidget(attrs={'class':'read-only'}
                           initial="$50")

Then in your CSS, do some styling for a read-only class, or you can adjust the attributes accordingly.

终止放荡 2024-12-28 15:46:18

只需执行以下操作:

class MyAdmin(admin.ModelAdmin):
    readonly_fields = ('foo',)

    def foo(self, obj):
        return '${0}'.format(obj.amount)

Just do something like:

class MyAdmin(admin.ModelAdmin):
    readonly_fields = ('foo',)

    def foo(self, obj):
        return '${0}'.format(obj.amount)
鸠书 2024-12-28 15:46:18

另一个更合适的解决方案适用于 Django 2.1.2:

如果我们查看 contents<,ModelAdmin 通过特殊包装器 AdminReadonlyField (django/contrib/admin/helpers.py) 呈现只读字段/code> 方法,我们可以看到
代码

if getattr(widget, 'read_only', False):
    return widget.render(field, value)

这意味着如果一个小部件具有值为 Trueread_only 属性
那么只读字段将调用小部件的 render 方法。
因此,您可以使用 render 方法来格式化您的值。

例如:

class CustomDateInput(widgets.DateInput):
    read_only = True

    def _render(self, template_name, context, renderer=None):
        return 'you value'

class CustomForm(forms.ModelForm):
    some_field = forms.DateTimeField(widget=CustomDateInput())

@admin.register(SomeModel)
class SomeModelAdmin(admin.ModelAdmin):
    form = CustomForm    
    readonly_fields = ['some_field']

Another, more appropriate solution, works in Django 2.1.2:

ModelAdmin renders read-only fields via special wrapper AdminReadonlyField (django/contrib/admin/helpers.py) if we look at contents method, we can see
the code

if getattr(widget, 'read_only', False):
    return widget.render(field, value)

It means that if a widget has read_only attribute with True value
then the read-only field will invoke widget's render method.
Hence, you can use render method to format your value.

For example:

class CustomDateInput(widgets.DateInput):
    read_only = True

    def _render(self, template_name, context, renderer=None):
        return 'you value'

class CustomForm(forms.ModelForm):
    some_field = forms.DateTimeField(widget=CustomDateInput())

@admin.register(SomeModel)
class SomeModelAdmin(admin.ModelAdmin):
    form = CustomForm    
    readonly_fields = ['some_field']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文