1

这是这个问题的延续,我试图弄清楚如何构造一个由 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))
4

1 回答 1

3

我建议save在每个表单上构建一个知道如何将自己保存到数据库的方法。它遵循“表单通过 form.save() 执行其操作”的一般模式,因此应该直观地遵循。

最重要的是,现在你有一个毯子:“对于所有表单中的每个字段,将 Thing 属性设置为这些字段”。

由于实际上您具有按表单保存的行为,因此我认为要求将实例传递给每个表单以便每个表单都有机会以适合其字段的方式保存数据是有意义的。

class Form1(...):
   def save(self, thing):
      for field, value in self.cleaned_data.items():
          setattr(thing, field, value)

class Form2(...):
   def save(self, thing):
      thing.point = Point.objects.get_or_create(lat=self.cleaned_data.get('lat'), long=...)
      # note, you may not want get_or_create if you don't want to share points.

您的视图将变为:

for form in form_list:
    form.save(instance)

只是一个想法。

如果您想更加干爽并且喜欢其他表单的自动化,我会构建一个基本表单,其中已经定义了一个保存方法:

class BaseSaveBehaviorForm(forms.Form):
     def save(self, thing):
         for field, value in self.cleaned_data.items():
             setattr(thing, field, value)

class NormalBehaviorForm(BaseSaveBehaviorForm):
     # your forms as usual


class SpecialSaveBehaviorForm(forms.Form):
     def save(self, instance):
         # do something unusual
于 2013-10-04T20:36:07.820 回答