Incorrect longitude/latitude saved to PointField via admin widget - django

I have a basic GeoDjango PointField:
point = models.PointField(srid=4326, null=True)
When using the admin, I would expect this to be saved in the database as (for example, London):
SRID=4326;POINT (-94.577597, 39.057294)
but instead, if I place the marker on London, I get:
SRID=4326;POINT (-19067.91721243037 6711435.410105047)
Where the longitude/latitude are way off.
I've tried manually setting the Point to the location:
obj.point = Point(-94.577597, 39.057294)
obj.save()
but the widget is then rendered way off.
So it seems that the wrong latitude and longitude are being saved to the field, or the wrong coordinate system is being used.
I've tried to manually override the widget to make sure the correct SRID is being used on the widget with:
class Meta:
model = models.MyModel
fields = "__all__"
widgets = {
'point': widgets.OSMWidget(attrs={
'map_srid': 4326,
'map_width': 800,
'map_height': 500,
'display_raw': True
})
}
but no luck.
I have all the dependencies for GeoDjango installed, I've enabled the postgis extension on my database and I the correct engine in use:
DATABASES['default']['ENGINE'] = 'django.contrib.gis.db.backends.postgis'
I also have added django.contrib.gis to INSTALLED_APPS. I'm using the default form widget, but I'm wondering might the widget be the problem?

OK, it seems I forgot to include the correct GeoDjango Admin class:
from django.contrib.gis import admin
from app import models
#admin.register(models.MyModel)
class MyAdmin(admin.OSMGeoAdmin):
pass

Related

GeoDjango Admin displaying openlayers map instead of open street map in Admin

i am have enabled everything needed to work with spatial data at the database and django setting level, my profile model has a default_location field that is a PointField. as shown below.
from django.contrib.gis import models
class Profile(models.Model):
...
default_location = models.PointField()
i registered the profile model as an inline to be viewed and edited from within a User model (one-to-one relationship between user and profile).code shown below
class ProfileInline(StackedInline):
model = models.Profile
class NewUserAdmin(admin.GISModelAdmin):
gis_widget = OSMWidget
inlines = [ProfileInline]
admin.site.unregister(models.User)
admin.site.register(models.User, NewUserAdmin)
however i keep getting a openlayer map in my django admin page
please can anyone suggest a fix to this. i need open street map because of it detailed street feature.
You can use the django-leaflet package. By default an OpenStreetMap is displayed, and it also has better tools and interface.
After installing you need add leaflet to INSTALLED_APPS in settings.py.
Then you use LeafletGeoAdmin in your ModelAdmin in admin.py.
You can add some customizations by adding this to your settings.py:
LEAFLET_CONFIG = {
'DEFAULT_CENTER': (39.694819, -8.130229),
'DEFAULT_ZOOM': 6,
'MAX_ZOOM': 20,
'MIN_ZOOM':3,
'SCALE': 'both'
}
More information here: https://django-leaflet.readthedocs.io/

How can I change the django-leaflet openlayers canvas?

I have a django-leaflet widget, but the widget shows some imagery instead of openlayers map, how can i change this on modelform?
from django import forms
from django.contrib.gis.forms import OSMWidget, PointField, ModelForm
from leaflet.forms.widgets import LeafletWidget
class YourMapWidget(LeafletWidget):
geometry_field_class = 'geom'
class ApartmentForm(forms.ModelForm):
class Meta:
model = Apartment
fields = '__all__'
widgets = {'geom': YourMapWidget()}
One way is to change it globally using the settings. If you want to use OSM, just add something like this to your settings,
LEAFLET_CONFIG = {
'DEFAULT_CENTER': (6.0, 45.0),
'DEFAULT_ZOOM': 16,
'TILES': 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'
}
BTW, it's actually weird, the default settings should be OSM map.
django-leaflet docs

how to show a google map view in django admin view for a polygon field

I've been trying to show google map view for a polygon field in my django admin view without using olwidget.
But against geom field I get a blank view. No map widget.
I am upgrading django version in my app from 1.3 to 1.10 and need to keep compatibility with both versions so cannot use olwidget(not compatible with django 1.10). I was using olwidget to show google map widget in django 1.3 version env.
In Django 1.10 there is a OSMGeoAdmin class but that doesn't give me the view that is in my second image link.
So, need a way to show google map view that works in both versions of django.
from django.contrib.gis.maps.google import GoogleMap
from django.contrib.gis.admin.options import GeoModelAdmin
from django.contrib.gis.db import models
class GoogleMapAdmin(GeoModelAdmin):
GMAP = GoogleMap(api_url="api-url")
map_template = 'gis/google/google-map.html'
class MyMapModelAdmin(GoogleMapAdmin):
list_display = ('name', )
list_map = ['geom']
class MyMapModel(models.Model):
name = models.CharField(max_length=100)
geom = models.PolygonField()
blank view
previous view using olwidget

Hiding a model field in Django admin 1.9

I have registered some models to display in the admin area, but I would like for some fields to be hidden.
As an example, I have a TeachingClasses model with a BooleanField named 'Status' that is set to True or False depending if the class is open or not. But that is set somewhere else in the app. There is no need to have that field displayed in the admin area when someone wants to create a new class to attend.
As such, is there a way to hide that field in the admin area?
I have tried adding this to the app admin.py file but it did nothing
from django.contrib import admin
class MyModelAdmin(admin.ModelAdmin):
class TeachingClasses:
exclude = ('Status',)
but it's not working?
Any clue if this is the right way?
My model:
class TeachingClasses(models.Model):
name = models.Charfield('Class Name',max_lenght=64)
[...]
status = models.BooleanField('Status',default=True)
What you did is not the correct syntax, you need:
class TeachingClassesAdmin(admin.ModelAdmin):
exclude = ('status',)
admin.site.register(TeachingClasses, TeachingClassesAdmin)
Django doc about how to use exclude.
In the admin.py:
class TeachingClassesAdmin(admin.ModelAdmin):
list_display = ('name',) # plus any other fields you'd like to display
admin.site.register(TeachingClasses, TeachingClassesAdmin)`

Django Admin: Add TinyMCE to particular TextField only?

I have set up TinyMCE to work with the Admin panel (as per the instructions in the Django Docs http://code.djangoproject.com/wiki/AddWYSIWYGEditor )
The problem is that I have Inlines and other text areas within my model for which I don't want TinyMCE to render
Does anyone know how to set TinyMCE to only load for particular fields within my model?
Thanks
EDIT
Ok, so I've installed django-tinymce and configured it
I have created the following in the admin.py of the model with the field I want to add tinymce to:
class FooAdminForm(forms.ModelForm):
class Meta:
model = Foo
def __init__(self, *args, **kwards):
self.bar = forms.TextField(widget=TinyMCE(attrs={'cols': 80, 'rows': 30}))
super(FooAdminForm, self).__init__(*args, **kwargs)
Unfortunately this still isn't working
Right, if anyone is looking to do this:
First make sure the tinymce settings are correct:
TINYMCE_JS_ROOT = '/media/tiny_mce/'
TINYMCE_JS_URL = os.path.join(MEDIA_URL, "tiny_mce/tiny_mce_src.js")
TINYMCE_DEFAULT_CONFIG = {
'plugins': "table,spellchecker,paste,searchreplace",
'theme': "advanced",
}
TINYMCE_SPELLCHECKER = True
Then in the admins.py of your model
from django.forms import *
from django.db.models import *
from tinymce.widgets import TinyMCE
class ProductionForm(forms.ModelForm):
some_field = forms.CharField(widget=TinyMCE(attrs={'cols': 80, 'rows': 10}))
class Meta:
model = Production
class ProductionAdmin(admin.ModelAdmin):
form = ProductionForm
That wiki page is about five years old (!) and these days there's a much easier way of integrating TinyMCE, by simply using the django-tinymce project.
However, since you've already done it this way, you can achieve what you want with a simple change to the textareas.js script. The method described at your link uses mode: textareas, which as you note converts all textareas automatically. What you want is this:
mode: "exact",
element: "id_mytextarea",
where "id_mytextarea" is the HTML ID of the field you do want to convert - usually the name of the model field prefixed by "id_". See the TinyMCE documentation.