Django Admin表单:设置只读字段的默认值 [英] Django Admin Form: Set the default value of a readonly field

查看:197
本文介绍了Django Admin表单:设置只读字段的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:在创建和更新资源时,在django管理表单上显示特定于请求的,只读的 user_id



此显示应为只读字段( readonly ,未禁用)。显示的 user_id 是从发出请求的用户( request.user )派生的,因此从中设置了初始值。 / p>

以下此帖子,我们只需在 get_form 方法中设置它,就像这样:

  def get_form(self,request,obj = None,* args,** kwargs):
form = super(ProfileAdmin,self).get_form(request,* args, ** kwargs)
form.base_fields ['user']。initial = request.user
返回表格

但是,当用户为 base_fields 时,它不再处于该状态。实际上,我在任何地方都找不到。有什么想法吗?



其他帖子建议保存,我打算这样做,但是我需要在此之前在表单上显示它。

解决方案

您是正确的,将字段设置为只读会将其从base_fields中排除。如文档


此选项中的任何字段(应该是列表或元组)将按原样显示其数据,并且不可编辑;


您可以像list_display一样为它定义一个方法,但是自从需要从请求中设置用户,我们需要有该方法中不可用的请求对象。



因此对于您的用例,我们可以使用<一个href = https://docs.djangoproject.com/zh-CN/2.1/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_for_foreignkey rel = noreferrer> formfield_for_foreignkey 方法如下:

  def formfield_for_foreignkey(self,db_field,request,** kwargs):
如果db_field.name =='lend_by' :
#设置请求对象中的用户
kwargs ['initial'] = request.user.id
#使字段变为只读
kwargs ['disabled'] = True
return super()。formfield_for_foreignkey(db_field,request,** kwargs)

希望它帮助。


THE GOAL: Display a request-specific, read-only user_id on a django admin form when creating and updating a resource.

This display should be a readonly field (readonly, NOT disabled). The user_id displayed is derived from the requesting user (request.user) so the initial value is set from that.

Following this post, we should simply set it in the get_form method like so:

def get_form(self, request, obj=None, *args, **kwargs):
    form = super(ProfileAdmin, self).get_form(request, *args, **kwargs)
    form.base_fields['user'].initial = request.user
    return form

However, user is no longer on the base_fields when it is readonly. In fact, I can't find it anywhere. Any thoughts?

Other posts suggest on save, which I intend to do, but I need it to show on the form before then.

解决方案

You are right that making the field readonly excludes it from the base_fields.As mentioned in the docs :

Any fields in this option (which should be a list or tuple) will display its data as-is and non-editable; they are also excluded from the ModelForm used for creating and editing.

You could have define a method for it just like list_display but since you need to set user from the request, we need to have the request object, which isn't available in the method.

So for your use case, we can use formfield_for_foreignkey method as :

def formfield_for_foreignkey(self, db_field, request, **kwargs):
    if db_field.name == 'lend_by':
        # setting the user from the request object
        kwargs['initial'] = request.user.id
        # making the field readonly
        kwargs['disabled'] = True
    return super().formfield_for_foreignkey(db_field, request, **kwargs)

Hope it helps.

这篇关于Django Admin表单:设置只读字段的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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