构造django形式的属性值
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了构造django形式的属性值相关的知识,希望对你有一定的参考价值。
如何将值传递给django表单中的属性构造函数?
换句话说,我有一个这样的表单,我想在实例化表单时设置SOMETHING。
class ImageUploadFileForm(forms.ModelForm):
class Meta:
model = Photo
fields = [ 'image' ]
image = cloudinary.forms.CloudinaryJsFileField({ 'public_id': SOMETHING })
即在视图中:
uploadform = ImageUploadFileForm(instance=whatever, something='BLAHBLAHBLAH')
我怀疑我错误地想到了这个......
Thx Shang Wang!
对于在CloudinaryJSFileField中搜索的所有人,不要忘记添加“选项”,如下所示:
class ImageUploadFileForm(ModelFormControlMixin):
class Meta:
model = Photo
fields = [ 'image' ]
def __init__(self, *args, **kwargs):
self.public_id = kwargs.pop('public_id')
super(ImageUploadFileForm, self).__init__(*args, **kwargs)
self.fields['image'] = cloudinary.forms.CloudinaryJsFileField(options={ 'public_id': str(self.public_id) })
答案
class ImageUploadFileForm(forms.ModelForm):
class Meta:
model = Photo
fields = [ 'image' ]
def __init__(self, *args, **kwargs):
self.something = kwargs.pop('something')
super(ImageUploadFileForm, self).__init__(*args, **kwargs)
self.fields['image'] = cloudinary.forms.CloudinaryJsFileField({ 'public_id': self.something })
然后
uploadform = ImageUploadFileForm(instance=whatever, something='BLAHBLAHBLAH')
将参数传递给表单构造函数的相当标准方法。
以上是关于构造django形式的属性值的主要内容,如果未能解决你的问题,请参考以下文章