2017-10-18 5 views
1

J'ai essayé de l'ajouter naïvement à admin.py comme je le ferais normalement.Changer la valeur par défaut GeoModelAdmin à OSMGeoAdmin

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

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

J'ai essayé spécifier le widget:

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

Mais il montre Stil l'image satellite par défaut de GeoModelAdmin.

Voici les modèles de base avec lesquels je travaille.

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") 
    ] 

Et la documentation Je suivais:

+2

L'argument '' widget' sur FieldPanel' doit être un objet widget ou class, plutôt qu'un chemin de chaîne - pouvez-vous essayer 'depuis django.contrib.gis.forms.widgets importer OSMWidget' et ensuite' FieldPanel ('location', widget = OSMWidget) '? – gasman

Répondre

1

@gasman est correct!

Si vous jetez un oeil à Django docs sur les specifying widgets vous verrez qu'un objet est passé et non une chaîne:

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

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

Merci. J'ai mis à jour le code. Je ne reçois aucune erreur, mais le widget ne change pas dans l'admin. –

+0

Cela n'a pas fonctionné car ProjectMap était un ParentalKey à ProjectPage. Si ce n'est pas le cas, cela fonctionne. –