如何获取单个小部件在Django中设置2个字段? [英] How to get a single widget to set 2 fields in Django?
问题描述
但是我可以有1个小部件(单个地图)设置2个不同的字段?
在表单上定义拉长字段;将它们设置为使用HiddenInputWidget。
在表单初始化后,添加另外一个包含您的自定义窗口小部件的字段,它包含两个值。
在该添加字段的clean方法中,设置self.cleaned_data ['lat']和self.cleaned_data ['lng']
可能有更清洁的方法,但它应该工作
I got a model with 2 fields: latitude and longitude. Right now they're 2 CharFields, but I want to make a custom widget to set it in admin - was thinking about displaying Google Maps, then getting the coordinates of the marker.
But can I have 1 widget (a single map) to set 2 different fields?
Define lat and long fields on your form; set them to use the HiddenInputWidget.
After the init of your form, add another field which contains your custom widget that takes two values.
In the clean method for that added field, set the values of self.cleaned_data['lat'] and self.cleaned_data['lng']
There are probably cleaner ways, but it should work
这篇关于如何获取单个小部件在Django中设置2个字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!