2017-10-18 225 views
1

我试图像我通常会天真地把它添加到admin.py更改默认GeoModelAdmin到OSMGeoAdmin

from django.contrib.gis import admin 
from project.models import ProjectMap 

admin.site.register(ProjectMap, admin.OSMGeoAdmin) 

我试图指定窗口小部件:

content_panels = Page.content_panels + [ 
    FieldPanel('location', widget='django.contrib.gis.forms.widgets.OSMWidget'), 
] 

但还是老样子表示从GeoModelAdmin默认的卫星图像。

这里是基本款我的工作。

class ProjectPage(Page): 
    date = models.DateField("Post date", null=True) 
    body = RichTextField(blank=True) 

    def main_image(self): 
     gallery_item = self.gallery_images.first() 
     if gallery_item: 
      return gallery_item.image 
     else: 
      return None 

    search_fields = Page.search_fields + [ 
     index.SearchField('body'), 
    ] 

    content_panels = Page.content_panels + [ 
     MultiFieldPanel([ 
      FieldPanel('date'), 
     ], heading="Project information"), 
     MultiFieldPanel([ 
      FieldPanel('body', classname="full"), 
     ], heading='Project'), 
     InlinePanel('gallery_images', label="Gallery images"), 
     InlinePanel('project_map', label="Project location") 
    ] 


class ProjectMap(Orderable): 
    page = ParentalKey(ProjectPage, related_name='project_map') 
    city = models.CharField(blank=True, max_length=250) 
    address = models.CharField(blank=True, max_length=250) 
    country = models.CharField(blank=True, max_length=250) 
    location = PointField(blank=True, null=True) 

    content_panels = Page.content_panels + [ 
     MultiFieldPanel([ 
      FieldPanel('city'), 
      FieldPanel('address'), 
      FieldPanel('country'), 
      FieldPanel('location'), 
     ], heading="Location") 
    ] 

而且我下面的文档:

+2

上'FieldPanel'的'widget'参数必须是控件对象或类,而不是字符串路径 - 你可以从django.contrib.gis.forms.widgets尝试'导入OSMWidget'然后'FieldPanel(“位置”,窗口小部件= OSMWidget)'? – gasman

回答

1

@gasman是正确的!

如果你看看Django文档约specifying widgets,你会看到一个对象传递,而不是一个字符串:

from django.contrib.gis.forms.widgets import OSMWidget 

content_panels = Page.content_panels + [FieldPanel('location', widget=OSMWidget),] 
+0

谢谢。我更新了代码。我没有收到任何错误,但该部件在管理员中未更改。 –

+0

它没有工作,因为ProjectMap是ParentalKey到ProjectPage。如果情况并非如此,那么它就有效。 –