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
Related
I want to show select box options as raido buttons in Django Admin change model view. I don't want to write custom model forms. I'm looking for a way to render some select boxes as radio buttons while keeping auto generated model forms of the Django admin. I'm using django v 1.11.
Assuming my_field is the field, we want to be rendered as Radio Button
# admin.py
from django.contrib import admin
from django import forms
from .models import MyModel
class MyModelAdminForm(forms.ModelForm):
class Meta:
model = MyModel
exclude = ()
widgets = {'my_field': forms.RadioSelect}
class MyModelAdmin(admin.ModelAdmin):
form = MyModelAdminForm
admin.site.register(MyModel, MyModelAdmin)
I've got a use case where I have multiple Ingredient that can be linked to a Recipe through the Django admin. Now I have around a hundred ingredients which makes it very difficult to select the ingredients in the following UI.
Is there a way to add a search field or something similar to the django admin for easier selection?
You have few choices.
1. filter_horizontal
With filter_horizontal, you can use horizontal m2m ui in admin. I prefer this way using m2m in admin.
class YourAdmin(admin.ModelAdmin):
filter_horizontal = ('m2m_field',)
...
And the result will be...
2. raw_id_fields docs
You can use raw_id_fields for using pop-up modal with your m2m fields.
It's bit useful when you have lots of m2m field. Also, it's easy to filter which m2m obj to add.
class YourAdmin(admin.ModelAdmin):
raw_id_fiedls = ('m2m_field',)
...
I suppose you want to filter over ingredients and select it one by one on admin UI
You can use django forms builtin CheckboxSelectMultiple
widget in place of SelectMultiple to make selection easy
from django import forms
from django.contrib import admin
class RecipeForm(forms.ModelForm):
class Meta(object):
model = Recipe
widgets = {
'Ingredient': forms.CheckboxSelectMultiple,
}
class RecipeAdmin(admin.ModelAdmin):
form = RecipeForm
admin.site.register(Recipe, RecipeAdmin)
Alternatively, you can use django-better-filter-widget
package if you want a search input on choices, Refer Github repo for
installation
It is a custom widget, created by overriding SelectMultiple widget of
django forms
from django import forms
from django.contrib import admin
from better_filter_widget import BetterFilterWidget
class RecipeForm(forms.ModelForm):
class Meta(object):
model = Recipe
widgets = {
'Ingredient': BetterFilterWidget(),
}
class RecipeAdmin(admin.ModelAdmin):
form = RecipeForm
admin.site.register(Recipe, RecipeAdmin)
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
I'm working in a form with a m2m field. I want that this field looks like the horizontal interface of the django admin site... ¿how i can do it?
thanks...
You need to use the FilteredSelectMultiple widget
from django.contrib.admin.widgets import FilteredSelectMultiple
from django import forms
from .models import Person
class PersonForm(forms.ModelForm):
some_field = forms.ModelMultipleChoiceField(Person.objects.all(), widget=FilteredSelectMultiple("Person", False, attrs={'rows':'2'}))
class Meta:
model = Person
You will also need to include the Javascript and CSS used in the admin. Here's an example
I've made some changes to an admin form so that I could display a TextField like a CharField but the form itself looks pretty ugly in the admin menu as the form elements aren't stretching properly. I also don't want to display the name of model when I print it since it's already on the page. How would I make those changes? Ideally I would like the link field to take up all the remaining space shown the screenshot below.
admin.py
from linkrotator.models import Link, LinkList
from django.contrib import admin
from django import forms
class LinkModelForm( forms.ModelForm ):
link = forms.CharField( label = "Link")
class Meta:
model = Link
class LinkInline(admin.TabularInline):
form = LinkModelForm
model = Link
class LinkListAdmin(admin.ModelAdmin):
inlines = ( LinkInline, )
admin.site.register(LinkList, LinkListAdmin)
How it looks.
You need to edit the CSS for the admin section, easymode:
http://docs.djangoproject.com/en/dev/ref/contrib/admin/#modeladmin-media-definitions