这是这个问题的延续,我试图弄清楚如何构造一个由 lat / lon FloatFields 组成的 PointField。我接受了@Simon 的建议并将我的模型重组为如下所示:
class Point(models.Model):
lat = models.FloatField()
lon = models.FloatField()
class Thing(models.Model):
point = models.ForeignKey(Point)
我的表单有两个字段对应于谷歌地图的经度和纬度坐标的值:
class StepThreeForm(forms.Form):
lat = forms.FloatField()
lon = forms.FloatField()
...
但是,由于明显的原因,这不起作用,但我不知道如何解决它。lat
为了澄清,我试图让两个表单字段对应于和的外键值lon
。这是补充信息(我使用的是 FormWizard 和 forms.Form):
url(r'^mapform/$', login_required(MyWizard.as_view([StepOneForm, StepTwoForm, StepThreeForm])), name='create'),
class MyWizard(SessionWizardView): ## this view also serves to edit existing objects and provide their instances
def done(self, form_list, **kwargs):
id = form_list[0].cleaned_data['id']
try:
thing = Thing.objects.get(pk=id)
instance = thing
except:
thing = None
instance = None
if thing and thing.user != self.request.user:
raise HttpResponseForbidden()
if not thing:
instance = Thing()
for form in form_list:
for field, value in form.cleaned_data.iteritems():
setattr(instance, field, value)
instance.user = self.request.user
instance.save()
return render_to_response('wizard-done.html', {
'form_data': [form.cleaned_data for form in form_list],})
我感谢任何和所有的建议和帮助!
编辑:根据 Yuji Tomita 的输入进行更新。其中大部分都很有意义(谢谢!),但我不确定它为什么会导致 ValueError。
class MyWizard(SessionWizardView):
....
for form in form_list:
form.save(instance)
...
class StepOneForm(forms.Form):
...
def save(self, thing):
for field, value in self.cleaned_data.items():
setattr(thing, field, value)
class StepTwoForm(forms.Form):
...
def save(self, thing):
for field, value in self.cleaned_data.items():
setattr(thing, field, value)
我相信我应该将表单字段保留为 lat 和 lon,因为我在表单中使用谷歌地图并从选定的输入中获取 lat 和 lon,然后从这些值构造一个点字段:
class StepThreeForm(forms.Form):
lat = forms.FloatField()
lon = forms.FloatField()
def save(self, thing):
thing.point = Point.objects.get_or_create(lat=self.cleaned_data.get('lat'), lon=self.cleaned_data.get('lon'))
这产生了一个ValueError: Cannot assign "(<Point: Point object>, False)": "Thing.point" must be a "Point" instance.
Traceback:
File "/lib/python2.7/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/lib/python2.7/django/contrib/auth/decorators.py" in _wrapped_view
20. return view_func(request, *args, **kwargs)
File "/lib/python2.7/django/views/generic/base.py" in view
48. return self.dispatch(request, *args, **kwargs)
File "/lib/python2.7/django/contrib/formtools/wizard/views.py" in dispatch
223. response = super(WizardView, self).dispatch(request, *args, **kwargs)
File "/lib/python2.7/django/views/generic/base.py" in dispatch
69. return handler(request, *args, **kwargs)
File "/lib/python2.7/django/contrib/formtools/wizard/views.py" in post
286. return self.render_done(form, **kwargs)
File "/lib/python2.7/django/contrib/formtools/wizard/views.py" in render_done
328. done_response = self.done(final_form_list, **kwargs)
File "/myproject/myapp/forms.py" in done
93. form.save(instance)
File "/myproject/myapp/forms.py" in save
67. thing.point = Thing.objects.get_or_create(lat=self.cleaned_data.get('lat'), lon=self.cleaned_data.get('lon'))
File "/lib/python2.7/django/db/models/fields/related.py" in __set__
366. self.field.name, self.field.rel.to._meta.object_name))