How to use
===========
Save the snippet to a file utils.py, and add the following view to your Django app:
from django.http import HttpResponse
from .utils import queryset_to_workbook
def download_workbook(request):
queryset = User.objects.all()
columns = (
'first_name',
'last_name',
'email',
'is_staff',
'groups')
workbook = queryset_to_workbook(queryset, columns)
response = HttpResponse(mimetype='application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment; filename="export.xls"'
workbook.save(response)
return response
Note: you can use dotted notation (`'foreign_key.foreign_key.field'`) in the columns parameter to access fields that are accessible through the objects returned by the queryset (in that case you probably want to use `select_related` with your queryset).
MultiForm and MultiModelForm
Based on a PrefixDict class I wrote and thus very lean. Lacks a little documentation, though
class MyMultiForm(ModelMultiForm):
class Meta:
localized_fields = '__all__'
form_classes = OrderedDict((
('form1', Form1),
('form2', Form2),
))
Subfields are named `form-name` `prefix_sep` `subfield-name`. `prefix_sep` defaults to `-`. For access in templates, use `form.varfields`, which uses `var_prefix_sep` (default: `_`), instead.
multiform.varfields()['form1_field1']
Custom model field to support two ways encryption.
The key is provided in the app settings.py file. It should be at least 32 chars.
** usage **
Assuming that you placed the new code into fields.py
from app.fields import EncyptedField
class Account(models.Model):
password = EncryptedField()
A very simple way of automatically loading data on model creation.
As I am using South I wanted an automatic way of loading initial data only when a new model is create during a migration.
Django provides almost everything out of the box by providing the *post_syncdb* signal (triggered also by South migrate command) and the *loaddata* command.
The code will simply look for a an existing fixture named `<model>_initial.*` and invoke the *loaddata* command to try to load it
Note: beware *post_syncdb* signal is deprecated since version 1.7: it has been replaced by *post_migrate*.
Based on [#2369](https://djangosnippets.org/snippets/2369/)
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=[...])]
**Designed to hold a list of pages and page ranges for a book/magazine index.**
A custom model field (and accompanying form field) that saves comma-separated pages and page ranges in human-readable string form. Includes some clean-up code, so that you can add a new page or range at the end of an existing entry, and it will put it in numeric order and combine runs into ranges. So this:
4-33, 43, 45, 60-65, 44, 59
becomes the tidy
4-33, 43-45, 59-65
**NOTE:** If you comment out the raising of the `ValidationError` in the form field's validate() method, it will actually clean up any extraneous characters for you (which could be dangerous, but for me is usually what I want), so even this horrible mess:
;4-33, 46a fads i44 ,p45o
gets cleaned to
4-33, 44-46
*This is the first custom field I've ever written for Django, so may be a little rough but seems to work fine.
jQuery code for making custom list on Admin page in DateTime widget.
Create new js file in your static folder with this code.
To use add custom js to Admin page like this:
class NiceAdmin(admin.ModelAdmin):
class Media:
js = ('js/adminNice.js',)
This code will change **all** DateTime widgets on selected page.
A simple way to force SSL on all pages. It's very simple at this point - the only issue I can see right now and I will address this later is if someone sends http:// in another portion of your url.