在Django模型表单中使用Model属性

时间:2022-07-14 19:35:51

I am attempting to use model properties like fields within a model form, but so far haven't had any luck. The result is that the form renders only the model fields, and not the property I defined. Any idea how to get the form to recognize the property added to the model? I expect to see the latitude property added as just another field in the form.

我试图使用模型属性,如模型形式中的字段,但到目前为止还没有任何运气。结果是表单只呈现模型字段,而不是我定义的属性。知道如何让表单识别添加到模型中的属性吗?我希望将latitude属性添加为表单中的另一个字段。

Models.py:

class Plot(models.Model):
        plot_id = models.AutoField(primary_key=True)
        plot_number = models.IntegerField(validators=[MinValueValidator(1)], null=False, unique=True)
        geometry = models.PointField(srid=2163, null=True, blank=True) 
        objects = models.GeoManager()

        @property
        def latitude(self):
            self.geometry.transform(4326)
            return self.geometry.y

        @latitude.setter
        def latitude(self, latitude):
            self.geometry.y = latitude

Forms.py:

class InventoryPlotForm(ModelForm):
    class Meta:
        model = ForestInventoryPlot
        exclude = {"geometry"}

2 个解决方案

#1


3  

Your property is not a Field instance, and as such a ModelForm cannot automatically generate a form field for it. You will need to explicitly define a latitude field on the InventoryPlotForm and manage its retrieval and update in the form's __init__ and save methods. Alternatively, you can implement your own Widget class and tell your InventoryPlotForm to use it for the geometry field:

您的属性不是Field实例,因此ModelForm无法自动为其生成表单字段。您需要在InventoryPlotForm上明确定义纬度字段,并在表单的__init__和save方法中管理其检索和更新。或者,您可以实现自己的Widget类,并告诉InventoryPlotForm将其用于几何字段:

class InventoryPlotForm(ModelForm):
    class Meta(object):
        widgets = {
            'geometry': MyGeometryWidget,
        }
        ...
    ...

#2


0  

Only Django Fields are automatically generated by ModelForm.

只有Django字段由ModelForm自动生成。

  • You need to define your field in the ModelForm
  • 您需要在ModelForm中定义您的字段

  • Probably make geometry field hidden
  • 可能会隐藏几何场

  • Write custom ModelForm Save method and separately save model fields and model properties.
  • 编写自定义ModelForm Save方法并单独保存模型字段和模型属性。

  • Modify your view to set the initial for the ModelForm
  • 修改视图以设置ModelForm的初始值

For example something like this:

例如这样的事情:

class InventoryPlotForm(ModelForm):
    latitude = forms.CharField(max_length=52)           # Something like this

    def save(self, commit=True):
        model_instance = super(InventoryPlotForm, self).save(commit=False)
        result = super(InventoryPlotForm, self).save(commit=True)
        model_instance.latitude = self.cleaned_data['latitude']
        model_instance.save()
        return result

    class Meta:
        model = ForestInventoryPlot
        widgets = {'geometry': HiddenInput()}

in your view

在你看来

...
form = InventoryPlotForm(instance=forestinventoryplot,
                         initial={"latitude":forestinventoryplot.latitude})
...

#1


3  

Your property is not a Field instance, and as such a ModelForm cannot automatically generate a form field for it. You will need to explicitly define a latitude field on the InventoryPlotForm and manage its retrieval and update in the form's __init__ and save methods. Alternatively, you can implement your own Widget class and tell your InventoryPlotForm to use it for the geometry field:

您的属性不是Field实例,因此ModelForm无法自动为其生成表单字段。您需要在InventoryPlotForm上明确定义纬度字段,并在表单的__init__和save方法中管理其检索和更新。或者,您可以实现自己的Widget类,并告诉InventoryPlotForm将其用于几何字段:

class InventoryPlotForm(ModelForm):
    class Meta(object):
        widgets = {
            'geometry': MyGeometryWidget,
        }
        ...
    ...

#2


0  

Only Django Fields are automatically generated by ModelForm.

只有Django字段由ModelForm自动生成。

  • You need to define your field in the ModelForm
  • 您需要在ModelForm中定义您的字段

  • Probably make geometry field hidden
  • 可能会隐藏几何场

  • Write custom ModelForm Save method and separately save model fields and model properties.
  • 编写自定义ModelForm Save方法并单独保存模型字段和模型属性。

  • Modify your view to set the initial for the ModelForm
  • 修改视图以设置ModelForm的初始值

For example something like this:

例如这样的事情:

class InventoryPlotForm(ModelForm):
    latitude = forms.CharField(max_length=52)           # Something like this

    def save(self, commit=True):
        model_instance = super(InventoryPlotForm, self).save(commit=False)
        result = super(InventoryPlotForm, self).save(commit=True)
        model_instance.latitude = self.cleaned_data['latitude']
        model_instance.save()
        return result

    class Meta:
        model = ForestInventoryPlot
        widgets = {'geometry': HiddenInput()}

in your view

在你看来

...
form = InventoryPlotForm(instance=forestinventoryplot,
                         initial={"latitude":forestinventoryplot.latitude})
...