So we recently upgraded to Django 1.4 and it's come to my attention that there was a change on how model fields with editable=False are handled with Model Forms.
所以我们最近升级到了Django 1.4,我注意到模型表格处理带有editable = False的模型字段的方式发生了变化。
If I have a user model with:
如果我有一个用户模型:
class User(models.Model):
id = UUIDField(primary_key=True, default=uuid4str)
name = models.CharField(max_length=128)
Where UUIDField automatically has the kwargs['editable'] = False, and then try to make a Model Form of that:
其中UUIDField自动拥有kwargs ['editable'] = False,然后尝试制作一个模型形式:
class UserFormSelf(ModelForm):
class Meta:
model = models.User
fields = ('id', 'name',)
widgets = {
'id': HiddenInput()
}
I get the following error when running:
运行时出现以下错误:
"Unknown field(s) (id) specified for User"
“为用户指定的未知字段(id)”
Per the docs, If False, the field will not be displayed in the admin or any other ModelForm. Default is True.. However, since the field is Hidden and only used so I can determine which User is being edited, there should be a way to allow it in the form, correct?
根据文档,如果为False,该字段将不会显示在管理员或任何其他ModelForm中。默认值为True ..但是,由于该字段是隐藏的并且仅使用,因此我可以确定正在编辑哪个用户,应该有一种方法在表单中允许它,对吗?
I really don't want id to be editable, however if this is the only solution I may have to. Perhaps it could be editable, but then on save if it isn't a new object, I induce an exception. Would like to figure out if it's possible to do it with editable=False first though.
我真的不希望id可以编辑,但是如果这是我可能必须的唯一解决方案。也许它可以编辑,但如果它不是一个新对象,那么我会引发异常。想知道是否有可能首先使用editable = False。
Thanks for your help!
谢谢你的帮助!
EDIT: Apparently it is not possible to check to see if the id field has been edited and write a custom "editable=False" on pre_save, as pre_save automatically thinks an object is new if it has a new id. If you can't provide an answer to the above, but know how to do this, please let me know as well.
编辑:显然无法检查id字段是否已编辑并在pre_save上写入自定义“editable = False”,因为pre_save会自动认为对象是新的,如果它有新ID。如果你不能提供上述答案,但知道如何做到这一点,请告诉我。
1 个解决方案
#1
1
Why not just display the value yourself? Forms are for input/output, and presumably your UUID will be calculated on your end or known to the application, if the record already exists. It's tough for me to give an example since you didn't provide your template code, but if you're using as_p()
to show your form, just add something like <p>Id: {{ object.id }}</p>
in the case when you're editing an existing object.
为什么不自己展示价值呢?表单用于输入/输出,如果记录已存在,可能您的UUID将在您的结尾计算或应用程序已知。由于您没有提供模板代码,因此我很难给出示例,但如果您使用as_p()来显示表单,只需添加类似
Id:{{object.id}} 在您编辑现有对象的情况下。
#1
1
Why not just display the value yourself? Forms are for input/output, and presumably your UUID will be calculated on your end or known to the application, if the record already exists. It's tough for me to give an example since you didn't provide your template code, but if you're using as_p()
to show your form, just add something like <p>Id: {{ object.id }}</p>
in the case when you're editing an existing object.
为什么不自己展示价值呢?表单用于输入/输出,如果记录已存在,可能您的UUID将在您的结尾计算或应用程序已知。由于您没有提供模板代码,因此我很难给出示例,但如果您使用as_p()来显示表单,只需添加类似
Id:{{object.id}} 在您编辑现有对象的情况下。