Extension of the idea from [WuzHere example from Google IO](http://code.google.com/p/wuzhere/) about creating one compressed js or css file. Original code used not very elegant if statements. I've changed it to template tags. On production server it will also use current version id.
Insert code in *cssjs.py* file in *templatetags* dir in your application and use as below (more details in docs):
`<script type="text/javascript" src="/media/jsmergefile.js"></script>`
**This code does not compress CSS and JS on the fly, because GAE is read-only and using Datastore is too heavy.**
Warning: This python script is designed for Django 0.96.
It exports data from models quite like the `dumpdata` command, and throws the
data to the standard output.
It fixes glitches with unicode/ascii characters. It looked like the 0.96
handles very badly unicode characters, unless you specify an argument that is
not available via the command line. The simple usage is:
$ python export_models.py -a <application1> [application2, application3...]
As a plus, it allows you to export only one or several models inside your
application, and not all of them:
$ python export_models.py application1.MyModelStuff application1.MyOtherModel
Of course, you can specify the output format (serializer) with the -f
(--format) option.
$ python export_models.py --format=xml application1.MyModel
This snippet shows how to disable fields in a edit page in the oldforms admin using jquery.
The idea is to add the javascript to the edit page using the `js` attribute of the model's `Admin` class. In this case jQuery and a custom javascript file are added.
The javascript sets the `disabled` attribute of the `name` field to `true` as soon as the document is ready.
I needed an abstract base class that can add attributes to the child classes based on the child's name. The attributes had to be implicit, but overridable, so all derived classes would get them by default, but they could be easily overriden in the child definition.
So, the code code I came up with basically consists of a customized metaclass used by the abstract model.
When using mysql the sql that is generated by syncdb doesn't create the foreign key relationship in all cases.
This code will run through a file called create_table.sql in which you store all your create sql statements ( use "python manage.py sqlall app1 app2 > create_table.sql" ) and outputs all the neccesary alter table scripts that add the foreign key. Its not 100% proof since the generated names can end up being more than 40 characters. Need to work on that.
I have [written](http://vidyanand.wordpress.com/2008/06/16/is-it-a-mysql-or-django-fault/) about it a little more in detail.
A lot of people new to Django don't realize that `manage.py` is [just a wrapper](http://www.djangoproject.com/documentation/django-admin/) around the `django-admin.py` script installed with Django and isn't needed.
(You may need to symlink `django-admin.py` to someplace in your system `PATH` such as `/usr/local/bin`.)
The most important thing it does is to set your `PYTHONPATH` and `DJANGO_SETTINGS_MODULE` environment variables before calling `django-admin.py`. Those same settings are needed when you move your site on to a production server like Apache, so it is important to know how they work.
This shell function sets those variables for you. Put it in your `.zshrc` or bash startup script. It works for both the monolithic project style and the lightweight app style of Django development [[1](http://www.pointy-stick.com/blog/2007/11/09/django-tip-developing-without-projects/)], [[2](http://www.b-list.org/weblog/2007/nov/09/projects/)].
This function isn't fancy; drop a comment if you have an improvement. Written for zsh and tested with bash 3.1.17.
Activate this middleware and define `LOG_FORMAT` & `LOG_ROOTS` in your `settings.py`. Then you can use Python's `logging` module for easy logging within your application.
This custom filter takes in a string and breaks it down by newline then makes each separate line an item in an unordered list.
Note that it does not add the <ul> </ul> tags to give the user a chance to add attributes to the list.
This is based of the 'linebreaks' filter built in django
I needed to sort a set of objects (a QuerySet for example) by an externally provided list of IDs - for example:
>>> writers = Writer.objects.all()
>>> sort_by_id_sequence(writers, [3, 1, 2])
[<Writer id: 3>, <Writer id: 1>, <Writer id: 2>]
I had the need to add `request.user` to the `extra_fields` argument of quite a few of my view based on [create_update for newforms snippet](http://www.djangosnippets.org/snippets/635/). I could have wraped the `create_object` function in my views.py, but as the logic is always the same, it seemed like a good idea to Not Repeat Myself.
You're looking at the top-rated snippets currently on the site; if you'd like to contribute, sign up for an account and you'll be able to rate any snippet you see.