Login

Tag "tabular-inlines"

Snippet List

Django admin inline ordering - javascript only implementation

Having spent ages trying out various admin inline ordering packages and examples I found on here and elsewhere I failed to find a single one that did what I was after in the way I wanted or that worked, so I wrote one! The general idea for this version was to be done purely in javascript, no additional methods or parameters required on your models, it's designed to be stuck in a js file and included in your admin class Media js parameter: class Media: js = ['js/admin/widget_ordering.js', ] Your model should have an integer column for sorting on, the name of this column should go in the 'sort_column' parameter at line 3 and your model should also obviously specify this in it's Meta 'ordering' class: class Meta: ordering = ('rank',) That's it! This is a pretty basic implementation that adds simple up and down buttons next to the sort order field, if you want to adapt this to use drag and drop or something, please feel free!

  • admin
  • sorting
  • ordering
  • inline
  • tabular-inlines
Read More

Readonly Tabluar Inline

An Image says more than 100 words: [readonlytabularinline.png](http://img39.imageshack.us/img39/6555/readonlytabularinline.png) Use `editable_fields` to exclude some fields from being readonly.

  • admin
  • tabular-inlines
Read More

GeoDjango maps in admin TabularInlines

This snippet adds support for OSM maps for GeometryField in Admin TabularInlines. The one possible issue with this snippet is that the OSMGeoInlineForm has to know about the parent ModelAdmin which it does through the code `model_admin_instance = admin.sites.site._registry[self.parent_model]` which won't work if you don't use the default model admin to register the model admin. I'll try and come up with a work around and edit the snippet. Due to the need to mess around with inline style sheets and IE not playing ball with just copying the innerHTML I've settled on using the jQuery.Rule plugin which I've included here as the last version published on the site was incompatible with jQuery 1.4.2 and I found a pathced version online, I also had to modify it due to the Django admin using the compatibility mode of jQuery so there is no global jQuery variable it's django.jQuery instead. 1. Create an osmgeo_inline.py file in your app and copy the top code into it. 2. Create the template file in a directory called admin within a template directory for your app, the template file must be called osmgeo_tabular_inline.html, and copy the middle code into it. 3. Create the jquery rule plugin file in your media or admin-media js directory and copy the bottom code into it. Don't forget to change the OSMGeoInlineForm's class Media's js = ('.....',) to the correct path to the file if need be. 4. In your admin.py you can create inline models using OSMGeoTabularInline just as you would TabularInline. Examples all based on the following in models.py from django.contrib.gis.db import models class MyModel(models.Model): name = models.CharField(max_length=64) route = models.LineStringField(srid=settings.WGS_SRID) class MySubModel(models.Model): mymodel = models.ForeignKey(MyModel) name = models.CharField(max_length=64) location = models.PointField(srid=settings.WGS_SRID) Example 1 - basic usage (admin.py): from django.contrib.gis.admin import OSMGeoAdmin from myapp.osmgeo_inline import OSMGeoTabularInline from myapp.models import MyModel, MySubModel class MySubModelInline(OSMGeoTabularInline): model = MySubModel class MyModelAdmin(OSMGeoAdmin): inlines = [MySubModelInline] admin.site.register(MyModel, MyModelAdmin) Example 2 - overriding the default map widget params and setting the inline map's centre point to match the main models map centre (admin.py): from django.contrib.gis.admin import OSMGeoAdmin from myapp.osmgeo_inline import OSMGeoTabularInline from myapp.models import MyModel, MySubModel class MySubModelInline(OSMGeoTabularInline): model = MySubModel class MyModelAdmin(OSMGeoAdmin): inlines = [MySubModelInline] params = {'map_width: 200, 'map_height': 200] def get_formset(self, request, obj=None, **kwargs): centre = None if obj is not None: centre = obj.route.centroid.transform(900913, clone=True) self.params['default_lon'] = centre.coords[0] self.params['default_lat'] = centre.coords[1] self.params['default_zoom'] = 12 return super(TrailSubmissionInlineBase, self).get_formset(request, obj, **kwargs) admin.site.register(MyModel, MyModelAdmin) I've not looked at StackedInlines because I don't use them but all that would be needed is to add a class OSMGeoStackedInline(StackedInline) that was a copy of OSMGeoTabularInline but with a template based on the StackedInline template - the javascript code in var initMap = function(row) would probably have to be adapted though.

  • django
  • admin
  • geodjango
  • map-widget
  • tabular-inlines
Read More

3 snippets posted so far.