A quick-and-dirty, and extremely simple, decorator to turn a simple function into a management command.
This still requires you to have the management directory structure, but allows you to name your primary method whatever you want, and encapsulates the basic functionality of an argument-accepting management commmand.
The function's docstring will be used for the command's help text if the `help` arg is not passed to the decorator.
Simple usage:
from myapp.utils import command
@command()
def my_command():
print "Hello, world"
I'm not too familiar with the intricacies of decorators and management commands, so this could probably (most likely) be improved upon, but it's a start.
**Update**:
I've taken this a bit farther and put my work up on bitbucket: https://bitbucket.org/eternicode/django-management-decorators/src
Who never wished to have valid address data by showing a little google map with a marker near the address input text ?
Using js only it gets quite easy.
This is a decorator which essentially replaces the decorated view with a view that always raises `Http404` (File Not Found) when `settings.DEBUG` is set to True.
You've filtered your changelist in your admin site and you want to edit a few entries here; you click on an object, edit it and press save, and you end up back at the default unfiltered changelist view. This ModelAdmin override is so that if you press "save" (not "save and add another", or "save and continue editing") you end up back at your filtered changelist.
There are other ways out there and other snippets to do similar; however I hadn't seen one to only redirect if you pressed save so this is what I came up with. Hopefully it's useful.
I wanted a way to deploy a Django site to both the root of a domain, and to a subdirectory. The solution was to loop over all urlpatterns and add a configurable string (URL_PREFIX) at the beginning of all patterns.
Forces admin site to use your custom login.
Very useful when using RemoteUserBackend.
See [here](http://djangosnippets.org/snippets/2128/) for a use case.
Auto-incremented primary keys are the default in Django and they are supported natively in most databases but for anything more complex things are less trivial. DB sequences are not standard, may not be available and even if they are, they are typically limited to simple integer sequence generators. This snippet bypasses the problem by allowing arbitrary auto-generated keys in Python.
The implementation needs to determine whether an IntegrityError was raised because of a duplicate primary key. Unfortunately this information is not readily available, it can be found only by sniffing the DB-specific error code and string. The current version works for MySQL (5.1 at least); comments about how to determine this in other databases will be incorporated in the snippet.
If you are like me and you find yourself often using M2M fields for tons of other on-model methods, in templates, and views alike, try using this quick and dirty caching. I show the use of a "through" model for the m2m, but that is purely optional. For example, let's say we need to do several different things with our list of beads, the old way is...
# views.py
necklace = Necklace.objects.get(id=1)
bead = Bead.objects.get(id=1)
if bead in necklace.beads.all():
# this bead is here!
# template necklace.html
{% for bead in necklace.beads.all %}
<li>{{ bead }}</li>
{% endfor %}
...which would hit the database twice. Instead, we do this:
# views.py
necklace = Necklace.objects.get(id=1)
bead = Bead.objects.get(id=1)
if bead in necklace.get_beads():
# this bead is here!
# template necklace.html
{% for bead in necklace.get_beads %}
<li>{{ bead }}</li>
{% endfor %}
Which only does one hit on the database. While we could have easily set the m2m query to a variable and passed it to the template to do the same thing, the great thing is how you can build extra methods on the model that use the m2m field but share the cache with anyone down the line using the same m2m field.
I'm by no means an expert, so if there is something here I've done foolishly, let me know.
This snippet is based on 928.
I've added the support to update a custom folder, using shell arguments.
It requires the argparse module.
You can install it with:
pip install argparse
Usage:
# Updates repositories in the specified <folder path>
# The default is the ./ folder
update_repos --path <folder path>
# List available options
update_repos -h
Alessandro Molari
The syndication documentation gives a very basic beginning to creating an iTunes Podcast Feed, but leaves a lot of work left to be figured out. This is a completed example. Because I needed to obtain the duration of the podcast file programmatically (not part of this code) I limit the podcast to mp3 files. If you want to obtain durations some other way you can set the mime_type in the same manner as the other properties.
This is what I'm using and should be plenty for you to customize from.
The admin site uses a CSS class for each kind of field so that they can be styled easily. This snippet gives ModelForms a similar feature.
See also: [James Bennett's explanation of formfield_callback](http://stackoverflow.com/questions/660929/how-do-you-change-the-default-widget-for-all-django-date-fields-in-a-modelform/661171#661171)
This middleware will prevent access to the admin if the users IP isn't in the INTERNAL_IPS setting, by comparing the request path with the reversed index URL of the default admin site, ultimately raising a 404 (unless DEBUG = True).
The important code really is just setting up the base site to use jquery and then using the javascript function to show the calendar on a widget with the .vDateField class set. The DateField modeltype automatically gets the css class .vDateField when using ModelForms.
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.