added commands:
altersql - shows sql code with alter queries
alterdb - apply alter queries. parameters:
--showsql - show queries
--app=APPLICATION - alter only selected application
[you need clone this repo](https://bitbucket.org/certator/django_snippets)
Simple middleware to complement the built in redirect middleware app. Add this after the contrib.redirect middleware - this will be fired if a 404 is triggered and the contrib.redirect fails to find a suitable redirect.
Useful if you want to add the redirects into the DB - and/or don't have access to the .htaccess script or whatever HTTP server based redirect machinery your site runs off.
You simply add in regex 'old_path' and 'new_path' entries into the same redirect app, but this will try to do a regex find and replace - i.e.
>>> r = new Redirect()
>>> r.old_path = '/my/test/path/([a-z]+)/with/regex/'
>>> r.new_path = '/my/result/path/$1/with/regex/'
>>> r.save()
this will convert:
/my/test/path/abcdef/with/regex/
into:
/my/result/path/abcdef/with/regex/'
also works with query strings:
/index.php?section=products
>>> old_path = '/index.php/\?section=([a-z]+)'
#need to add in the forward slash if ADD_SLASHES = True in your settings, and escape the question mark.
>>> new_path = '/section/$1/'
converts the url to '/section/products/'
Django later than 1.3 (not sure of exact version) wasn't using prefix settings in cache tags or functions used in views. Just for whole page caching. This is small custom cache snippet based on memcached.CacheClass. Feel free adding any comments.
As the title does a pretty good job of condensing, this is a subclass of `FieldListFilter` for the Django 1.4 Admin system which allows you filter by whether a field is or is not `NULL`.
For example, if you had an `Author` model and wanted to filter it by whether authors were also users of the site, you could add this to your `AuthorAdmin` class:
list_filter = (
('user_acct', IsNullFieldListFilter),
)
For the record, it began life as a modified version of `BooleanFieldListFilter` from `django.contrib.admin.filters`.
Completely based on [snippet 2729](http://djangosnippets.org/snippets/2729/) (see that snippet for useful comments!).
The above snippet did not work for me (something with MemoryError), so I looked at the Drula source code and reimplemented...
I'm working on a Project where on certain places I need absolute URL's, in development mode I need the port 8000 added to any absolute url.
This piece of work, took me some time to figure out. Couldn't find something similiar on the net, it's based on Code from the Python urlparse module.
You can change the "settings.PORT" part to "settings.DEBUG == True" if you like, and so on.
META: replace parameters in URL, edit parameters in URL, edit URL, urlparse
Sometimes the related objects needs that the main object exists in order to edit and save them properly.
There are two main solutions: override the ModelAdmin.add_view() view or remove the inlines only from the add view page (and not from the change page). The former requires a lot of coding, the latter it's impossible without patching Django because the inlines are not dynamic.
**This simple solution hides the inline formsets only from the add page, and not from the change page.** Adding an "if" structure it is possible to choose the inlines to use.
Example use case: when a related inline model have to save a file to a path that needs the ID key of the main model, this solution prevent the user to use the related inline model until the model it's saved.
Tested on Django-1.4, should work since Django-1.2.
SELECT FOR UPDATE, which does row-level locking in the database, was added by Django only in version 1.4. This snippet emulates that feature in older versions of Django. Tested in Django 1.2, but should work in newer versions as well.
select_related is removed because it causes errors when used with RawQuerySet.
You're looking at the most-bookmarked snippets on the site; if you'd like to help useful snippets show up here, sign up for an account and you'll get your own bookmarks list.