Login

Tag "admin"

Snippet List

models ColorField with clean minimal widget

A simple model ColorField that allows picking from a predefined list (currently picked up from settings.py The widget displays as a row of coloured SPAN's with the hex code inside. Simply click to choose a color. (requires jQuery in the page assigned to it's normal $ shortcut. Easy to change this is you don't use jQuery in this way)

  • models
  • admin
  • jquery
  • widget
  • custom-field
  • modelfield
Read More

When saving in admin it will call model save() twice.

When I save this in admin, it will call model save() twice Django version 1.2.3, using settings 'admin.development' Development server is running at http://127.0.0.1:8080/ Quit the server with CONTROL-C. [18/May/2011 10:08:22] "GET /admin/onixcodes/bookbatch/ HTTP/1.1" 200 38211 [18/May/2011 10:08:22] "GET /admin/jsi18n/ HTTP/1.1" 200 4256 [18/May/2011 10:08:29] "GET /admin/onixcodes/bookbatch/55/ HTTP/1.1" 200 9811 [18/May/2011 10:08:29] "GET /admin/jsi18n/ HTTP/1.1" 200 4256 save!! save!! [18/May/2011 10:08:33] "POST /admin/onixcodes/bookbatch/55/ HTTP/1.1" 302 0 [18/May/2011 10:08:33] "GET /admin/onixcodes/bookbatch/ HTTP/1.1" 200 38364 [18/May/2011 10:08:33] "GET /admin/jsi18n/ HTTP/1.1" 200 4256 Anyone has any suggestion? Thanks.

  • admin
  • save()
  • twice
Read More

Image Preview on ImageField in admin

This makes a 100x100 thumbnail of the image for the image field and shows it in the admin form combination of http://djangosnippets.org/snippets/955/ and http://www.psychicorigami.com/2009/06/20/django-simple-admin-imagefield-thumbnail/

  • admin
  • widgets
  • widget
  • image-previews
  • previews-on-ImageField
Read More

Admin: return to change_list with filter and pagination applied

By default every time you change and save an object in the admin, the change_list "jumps" to the first page, so filters you used to find the object (or the pagination-page) have to be applied again. If you have to go through a multi-object-list step-by-step this could become really annoying. The above snippet changes this behaviour by returning to the referring URL when saving. Included in this URL are variables for the filters/pagination. The snippet is part of your custom Model.admin in admin.py.

  • filter
  • admin
  • pagination
  • change_list
Read More
Author: fx
  • 4
  • 8

remove the annoying "Hold down control..." messages

This function mangles a generated form class to remove the Hold down "Control", or "Command"... messages from the help text. This is really a dirty hack awaiting a proper solution to [Django ticket 9321](http://code.djangoproject.com/ticket/9321). This function can be useful for forms in the admin interface that use custom widgets. Basic usage: class MyModelForm(forms.ModelForm): class Meta: model = MyModel class MyAdmin(admin.ModelAdmin): form = remove_holddown(MyModelForm, ('field1', 'field2'))

  • admin
  • forms
  • hack
  • widget
Read More

Admin action for a generic "CSV Export"

Based on [#2020](/snippets/2020/) Save the snippet as actions.py within your django app, and then add an action on any model you want in it's ModelAdmin definition. Example usage: from actions import export_as_csv_action class YourModelAdmin(admin.ModelAdmin): list_display = (...) list_filter = [...] actions = [export_as_csv_action("CSV Export", fields=[...])] * - Added UTF-8 encoding support * - Bugs fixed

  • admin
  • export
  • csv
  • action
Read More

restrict user access to modeladmin via metaclass

* Include `__metaclass__ = user_lock` to your ModelAdmin class * Add `__target__ = path_to_user_field` somewhere in the ModelAdmin * done! `__target__` is what will be used in the `filter` call. examples `'user'` or `'author'` or `'message__user'` e.t.c. The result of `__target__` is the field that is then checked against `request.user`

  • admin
  • user
  • restrict
  • model-admin
Read More

Group results by a range of dates in admin sidebar with calendar

Adds filtering by ranges of dates in the admin filter sidebar. [https://github.com/coolchevy/django-datefilterspec](https://github.com/coolchevy/django-datefilterspec) [http://coolchevy.org.ua](http://coolchevy.org.ua) https://github.com/coolchevy/django-datefilterspec/raw/master/datefilter.png Example: ` from django.db import models import datefilterspec class Person(models.Model): date = models.DateTimeField(default=datetime.datetime.now) date.date_filter = True class Admin: list_filter = ['date']

  • filter
  • admin
  • date
  • calendar
  • sidebar
  • filterspec
Read More

Advanced Search in django admin

This is an example on how to create a custom advanced search (like the google advanced search page) in the admin app instead of the basic search field. you need to override 2 template of the admin so in your templates/admin folder copy the html files from the original admin app as follow: change_list.html: add the name of the module of the templatetag at the top where there is the load command from: {% load adminmedia admin_list i18n %} to: {% load adminmedia admin_list custom_search_form i18n %} and on line 87 (circa) you need to load our custom template tag something like: {% block search %}{% advanced_search_form cl %}{% endblock %} search_form.html: put your form inside the <form> tags with {{asf}} should be enough (I have also removed cl.value from the input text field because would be to complex to reload form values too ) probably would be better to create 2 new clean template and extend them but I'll leave that as an exercise for the reader :) many thanks to [Peter Baumgartner to get me in the right direction after his post](http://lincolnloop.com/blog/2011/jan/11/custom-filters-django-admin/) Currently ordering in the admin panel doesn't work if you find a way to get it work please write a comment

  • admin
  • advanced-search
  • model-filtering
Read More

Memento

This class tracks changes in Django Admin. When a save action is performed, it stores the value of the old object using the Memento model. Example of code for a model in **admin.py** for a custom 'app': from app.memento.models import Memento def save_model(self, request, obj, form, change): obj.save() data = serializers.serialize("json", [obj, ]) m = Memento(app="Unidade",model=modelName,data=data, user=request.user) m.save() class UnidadeAdmin(admin.ModelAdmin): pass UnidadeAdmin.save_model = save_model This stores the former values of 'Unidade' model on 'Memento' model data. Not tested on previous versions of Django, but could work on them too.

  • admin
  • register
  • changes
  • persistence
  • tracking
Read More

Drag and drop ordering of admin list elements for Grappelli

Adds drag-and-drop ordering of rows in the admin list view for [Grappelli](http://code.google.com/p/django-grappelli/). This is based on [Snippet #2057](http://djangosnippets.org/snippets/2057/) and fixes some bugs as well as switching to jQuery/jQuery UI provided by Grappelli. No additional files need to be installed. The model needs to have a field holding the position and that field has to be made list_editable in the ModelAdmin. The changes of the ordering are applied after clicking 'Save'.

  • admin
  • sort
  • jquery
  • order
  • sortable
  • grappelli
Read More

Thumbnails in admin using django-thumbnails-works

Very straightforward way to display a thumbnail in the admin using [django-thumbnails-works](http://pypi.python.org/pypi/django-thumbnail-works) . django-thumbnails-works requires [cropresize](http://pypi.python.org/pypi/cropresize/#downloadsInstaller) (which requires and installs PIL). Add 'thumbnail_works'to INSTALLED_APPS in settings.py and here you go. Tested in django 1.3 alpha.

  • image
  • admin
  • thumbnail
Read More

Django Admin Replacer Code

Ok let's descrive what i have done I subclassed the django admin to create a form that makes you choose if activate a delete and replace login inside your admin. Then i have added a form with a modelChoiceField to make you select another model instance when you are selecting an istance to delete. If you select another instance the current instance will be replaced.

  • admin
  • object
  • delete
  • modeladmin
  • replacer
Read More

241 snippets posted so far.