Snippet List
Save the snippet as actions.py within your core 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 = ('field', 'get_field2')`
` actions = [export_to_csv(filename='your-model')]`
` def get_field2(self, obj):`
` return obj.field2`
- admin
- unicode
- export
- csv
- action
For a model FK'd to a user or profile, you might want every instance of that thing to appear in your site and in the admin as say "Joe's bucketlist." But if Joe's name is Ross, you want "Ross' bucketlist", not "Ross's bucketlist."
This snippet is for resolve the Django-PyAMF unicode problems, through the django force_unicode function called recursively, with a tuple of different charsets.
- unicode
- utf-8
- object
- latin-1
- force_unicode
- pyamf
Add's updated and created fields to a model if mixed in.
Example that uses the name as the representation:
class Company(models.Model, UnicodeReprMixIn):
"""
A representation of a comic book company.
"""
name = models.CharField(max_length=255)
slug = models.SlugField()
logo = models.ImageField(upload_to=os.path.join('upload', 'company_logos'))
url = models.URLField(verify_exists=True)
_unicode = "name"
- model
- unicode
- orm
- mixin
- __unicode__
Exporting unicode data to Excel in a CSV file is surprisingly difficult. After much experimentation, it turns out the magic combination is UTF-16, a byte order mark and tab-delimiters. This snippet provides two classes - UnicodeWriter and UnicodeDictWriter - which can be used to output Excel-compatible CSV.
There is a commonly encountered problem with Django and character sets. Windows applications such as Word/Outlook add characters that are not valid ISO-8859-1 and this results in problems saving a Django model to a database with a Latin 1 encoding. These characters should also be converted to avoid any display issues for the end users even if you are using a UTF-8 database encoding.
The topic is well covered at [Effbot](http://effbot.org/zone/unicode-gremlins.htm) and contains a list of appropriate conversions for each of hte problem characters.
Correcting this for all of your Django models is another issue. Do you handle the re-encoding during the form validation? The save for each model? Create a base class that all your models need to inherit from?
The simplest solution I have created leverages [Signals](http://code.djangoproject.com/wiki/Signals)
Combining the re-encoding method suggested at Effbot and the pre_save signal gives you the ability to convert all the problem characters right before the save occurs for any model.
**kill_gremlins method replaced with Gabor's suggestion**
- django
- python
- unicode
- latin1
- character
- encoding
Unicode is great, but there are places where the conversion ends up with unintelligible characters. I first noticed this with curly quotes entered in forms on our site.
`unicode_to_ascii` converts compound characters to close approximations in ASCII: such as umlaut-u to u, 1/2 (fraction glyph) to 1/2. You can add additional mappings in CHAR_REPLACEMENTS.
I ran into this because my development system is django on python 2.4 and I deploy to 2.3. It's a corner case where you use gettext, the
\# -\*- coding: utf-8 -\*-
header and want to have a consistant style in your file.
It is encouraged to use the unicode marker like u'string', but this does not work for __str__ methods of your models as they are called by the ``str'' function and that function again can not handle unicode. It would be really nice to have all unicode intern and only do the appropriated encoding on the output.
Anyway. With this little helper you can clutter your files with _('stirng of heart with € äüöß') ... With the coding directive in the header python 2.4 and gettext can handle this on 2.3 though they can't. So this script adds a parachut to the gettext wrapper that kicks in if gettext is failing.
- unicode
- gettext
- utf-8
- 2.4
- 2.3
9 snippets posted so far.