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/
Related
I have a model that I only want to use one row of its table. So, on admin, I would like to remove the list and add pages, and only edit the existing object. The model is this:
from django.db import models
class Banner(models.Model):
pass
class BannerImg(models.Model):
img = models.ImageField(upload_to="banners")
banner = models.ForeignKey(Banner, on_delete=models.CASCADE)
Basically, the Banner(pk=1) will be loaded by the frontend to display a landing page hero slider. I want multiple images, but also want them to be on the same admin form, so one could order, add or remove images from the same place. Of course having to Banner objects, wouldn't make sense in this case.
I can use inline fields to do the form, but how can I achieve the pages functionality (going directly to edit)?
Thanks!
Accordion to documentation.
from django.contrib import admin
class BannerImgInline(admin.TabularInline):
model = BannerImg
class BannerAdmin(admin.ModelAdmin):
inlines = [
BannerImgInline,
]
I am building an app in Django.
I found there is a very easy way to integrate a widget into django admin that allows the admin to filter model objects by fields values. That is achieved by including the line
list_filter = ['field_to_filter_by_its_values']
into the class mymodelAdmin(ImportExportModelAdmin) in admin.py, as shown below
class target_area_history_dataAdmin(ImportExportModelAdmin):
resource_class = target_area_history_dataResource
list_filter = ['Target_area_input_data__Name']
admin.site.register(target_area_history_data, target_area_history_dataAdmin)
Now, instead of integrate a widget to filter my model objects by that field, is there a way to integrate a widget to sort my model objects by that field?
Note: I am using Django Import-Export in my model.
I'll suggest you use django-treebeard. This allows you to view tree nodes hierarchically in the administration interface, with interface features dependent upon the tree algorithm used.
# admin.py
from django.contrib import admin
from treebeard.admin import TreeAdmin
from .models import Category
class CategoryAdmin(TreeAdmin):
list_display = ("title", "created", "modified",)
list_filter = ("created",)
admin.site.register(Category, CategoryAdmin)
What's cool about this is that you can not only sort (by clicking the header row) but also drag things around, as shown in this image.
I recommend you using the grapelli admin interface that allows what you need and a bit more. here you have the grapelli project page and the https://github.com/sehmaschine/django-grappelli.
It's a well documented package and is plug and play for what you need. It also gives a fresh face to Django Admin and is compatible with Django import/export package.
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 am building out a restaurant website and using Wagtail CMS Snippets for the owner to manage menu items. The list of menu items are getting rather long and I was wondering if there is any way to add a search input field to the Snippets admin window? Below is an annotated screenshot for visual reference. Thank you.
This can easily be solved by using Wagtail's ModelAdmin module (http://docs.wagtail.io/en/v1.8.1/reference/contrib/modeladmin/), all you need is to add this piece of code to your wagtail_hooks.py file:
from wagtail.contrib.modeladmin.options import (
ModelAdmin, modeladmin_register)
from .models import Product
class ProductAdmin(ModelAdmin):
model = Product
menu_label = 'Product' # ditch this to use verbose_name_plural from model
menu_icon = 'date' # change as required
menu_order = 200 # will put in 3rd place (000 being 1st, 100 2nd)
add_to_settings_menu = False # or True to add your model to the Settings sub-menu
exclude_from_explorer = False # or True to exclude pages of this type from Wagtail's explorer view
list_display = ('title', 'example_field2', 'example_field3', 'live')
list_filter = ('live', 'example_field2', 'example_field3')
search_fields = ('title',)
# Now you just need to register your customised ModelAdmin class with Wagtail
modeladmin_register(ProductAdmin)
It'll create a separate menu entry for your Products model that's customisable much like default Django Admin listing. Which means you can easily add different filters and sorters to a listing.
This is a very powerful feature and I myself don't show clients the "Snippets" section at all; it's just too simple and ugly. Instead, I create a separate ModelAdmin per snippet and this gives me the power of customisation.
The search bar will appear automatically once you set up your model to be indexed with the search system. You can do this by inheriting from the wagtail.wagtailsearch.index.Indexed class and defining a search_fields list on your model, as described here: http://docs.wagtail.io/en/v1.8.1/topics/search/indexing.html#wagtailsearch-indexing-models
(Note that if you're using Elasticsearch, you'll also need to run ./manage.py update_index to add the items to the search index.)
Today I decided to use an django-geoposition app to show some maps in my project. The problem is that while reading the Docs on github I still don't know what should I write in the admin.py so I could add some maps. Can someone help?
Docs: GitHub of django-geoposition
According to the Documentation, you just have to add a GeopositionField in an attribute:
from django.db import models
from geoposition.fields import GeopositionField
class YourModel(models.Model):
name = models.CharField(max_length=100)
map_field = GeopositionField()
So when you go to the model-page on the admin, you can see a map that you can search a location and get the latitude and longitude.
And if you want to allow choosing a location by clicking on the map, just add a few lines on the settings.py:
GEOPOSITION_MARKER_OPTIONS = {
'cursor': 'move'
}
https://github.com/philippbosch/django-geoposition
That's all.