It took me some time to figure out how to set up logging in Django. What I want to do is to log to a pair of rotating files each 1MB in size.
What I come up with is this code segment in the `settings.py` file.
My Models has a FK to translations and also a many 2 many to categories which also them are translated
With this code I concatenate the translation of the categories and allow the changelist to order them.
works only on mysql but you can adapt to your DB
SET SESSION is required by mysql.
This code will work on any model using correct address data in its fields that also require latitude and longitude data to be updated on saving.
It uses pythons own default urllib and json, so no need to install 3rd party stuff to make it work.
This method is preferred to getting it on the fly, due to the OVER_QUERY_LIMIT that you will get when parsing many address, this way means it stays up to day in the model and will update when any part of the address changes.
Simplified version of the snippet that renders model to PDF [http://djangosnippets.org/snippets/2540/](http://djangosnippets.org/snippets/2540/)
This PDF view mixin for Django Class Based Views.
See working project example: https://github.com/elena/django-pdfmixin-example
---
This is based on the case scenario where you have a model which has a `DetailView`.
You then construct a bespoke PDF for the same model that is unrelated in any way to the `DetailView`.
The PDF needs to be returned as a `HTTPResponse` object. The model object is provided.
Sometimes we may need to generate a *ModelChoiceField* in which choices are generated at runtime, depending on the locale language. The snippet generates a *ChoiceField* based on a queryset and a specific attribute of the Model, ordering the choices by the attribute content in the locale language.
**Usage example** (inside a form declaration)
country = LazyModelChoiceField(sort_by='name', queryset = \
Country.objects.all, empty_label=_('All countries'), label=_('Country'))
Based on lsbardel LazyChoiceField implementation (snippet 1767)
Helper functions to use the Google Visualization API in Django. Module stores queries to use and creates a JSon representation of a Google Visualization DataTable for each by inspecting the query's model. Looks up any choices defined for the columns.
Depends on Google Visualization Python API:
https://code.google.com/p/google-visualization-python/
To use, define the queries to use with the Visualization API at the head of views.py with:
add_query(queryset, 'query name')
then in view functions, get the JSon representation of a Google Visualization DataTable for the query with:
my_json = get_viz_json('query name')
Within the HTML template, pipe the JSon through safe:
var dataTable = new google.visualization.DataTable( {{ my_json|safe }} );
This is a simple logging [filter](https://docs.djangoproject.com/en/1.5/topics/logging/#topic-logging-parts-filters) to ensure that user-entered passwords aren't recorded in the log or emailed to admins as part of the request data if an error occurs during registration/login.
This is a modification of http://djangosnippets.org/snippets/1707/ that handles the database going down or PG Bouncer killing the connection. This also works in things like Twisted to make sure the connection is alive before doing a real query. Thanks @mike_tk for the original post!
EDIT: Updated the wrapper to handle multi-db. Before it was using the first connection it made, now it creates an attribute name for the connection based on the name of the database.
based on #2020
This one is even more generic than the previous generic ones since you can specify in fields any attribute you will give to the admin interface and not just fields from the model.
You can for example directly export the list_display as a list of fields, including look-ups for related attributes that you may have defined in a function inside the Admin Class
A simple example to show how to manage an history of memo-on-save.
Each time the user saves the model, he must provide a memo message.
The full memos history is shown as a read-only tabular-inline.
For each memo, user and date are also registered
([Django-Admin-Collapsed-Inlines](https://github.com/virajkanwade/Django-Admin-Collapsed-Inlines) could be usefull)