GeoDjango PointField覆盖默认小部件

时间:2020-06-12 15:41:01

标签: python django python-3.x widget geodjango

这是我做表单(forms.Form)时的样子。

#This one is not linked to a model but the map renders correctly. 
class OsmPointWidget(floppyforms.gis.PointWidget, floppyforms.gis.BaseOsmWidget):
    pass

class ContactForm(forms.Form):
    fields = ('User', 'Residential', 'Location')
    User = forms.CharField(widget=forms.TextInput())
    Residential = forms.CharField(widget=forms.TextInput())
    Location = floppyforms.gis.PointField(widget=OsmPointWidget(attrs={
        'map_width': 300,
        'map_height': 300,
            }))

enter image description here

这是我做表单时的样子(model.ModelForm)。

#This one is  linked to a model but the map renders incorrectly. 
class OsmPointWidget(floppyforms.gis.PointWidget, floppyforms.gis.BaseOsmWidget):
    pass

class ContactForm(forms.ModelForm):
    class Meta:
        model = Property
        fields = ('User', 'Residential', 'Location')
        Location = floppyforms.gis.PointField(widget=OsmPointWidget(attrs={
            'map_width': 300,
            'map_height': 300,
            }))

enter image description here

我试图覆盖默认的小部件。要么没有用。还是那不是解决我问题的正确方法。

from django.contrib.gis.admin import OSMGeoAdmin
from properties.models import Property
from django.contrib import admin
from django.contrib.gis.db import models
from properties.forms import OsmPointWidget



#In the brackets put the name of the model you are importing
#In localhost/admin they will add an S onto all the model names

admin.site.register(Property)


class PropertyAdmin(OSMGeoAdmin):
    formfield_overrides = {
        models.PointField: {'widget': OsmPointWidget},
    }
    list_display = ('name', 'location')

更新-现在可以使用

我在admin.py和forms.py中抓取了以下更改。

from properties.models import Property
from django import forms
import floppyforms as floppyforms 
from django.forms import Textarea
from django.forms import ModelForm

#This one is  linked to a model but the map renders incorrectly. 
class OsmPointWidget(floppyforms.gis.PointWidget, floppyforms.gis.BaseOsmWidget):
    pass

class ContactForm(forms.ModelForm):
    class Meta:
        model = Property
        fields = ('User', 'Residential', 'Latitude', 'Longitude', 'Location')  
        widgets = { 'Location' : OsmPointWidget(attrs={
         'map_width': 300,
         'map_height': 300})}

0 个答案:

没有答案
相关问题