Django模型中隐藏的字段 [英] Hidden field in Django Model

查看:488
本文介绍了Django模型中隐藏的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间,我做了一个Model类。我做了几个ModelForms,它工作得很好。



我最近不得不添加另一个可选(blank = True,null = True)字段,所以我们可以存储一些用户之间的关系数据。它本质上是一个推荐系统。



问题是添加这个新字段意味着引用字段显示在哪里我没有更改ModelForms以排除它。通常这只是意味着额外的10分钟通过和排除他们,但在这种情况下,由于项目管理政治无法控制,我只能控制这个应用程序的模型。



我可以:




  • 将字段设置为自动排除?

  • 将它设置为隐藏(可接受,如果不完美)?


解决方案

文档使用表单上的字段子集


在模型字段上设置editable = False。因此,通过ModelForm从模型创建的任何表单都不会包含该字段。



A while back I made a Model class. I made several ModelForms for it and it worked beautifully.

I recently had to add another optional (blank=True, null=True) field to it so we can store some relationship data between Users. It's essentially a referral system.

The problem is adding this new field has meant the referral field shows up where I haven't changed the ModelForms to exclude it. Normally this would just mean an extra 10 minutes going through and excluding them but in this case, due to project management politics out of my control, I only have control over the Models for this application.

Can I either:

  • Set the field to auto-exclude?
  • Set it so it renders as a hidden (acceptable if not perfect)?

解决方案

from the docs on Using a subset of fields on the form:

Set editable=False on the model field. As a result, any form created from the model via ModelForm will not include that field.

这篇关于Django模型中隐藏的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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