This is a slightly different and extendend version of this snippet:
http://www.djangosnippets.org/snippets/260/
Unique constraints for single fields are validated in a clean_FIELD, instead of globally in the form's clean() method, so that the error messages are correctly assigned to each field.
Additionally, you can specify mappings for unique_together constraints to assign those error messages to a specific field as well (instead of having them in non_field_errors(), where they would normally be.
This is a bit of a hack, but as far as I can see currently the only way to specify a validation error that is specific to a field in form.clean().
I am aware of clean_<fieldname>, but those are difficult to use when the validation process for a field involves other fields as well, because the necessary data might at that point not be yet available in form.cleaned_data.
Ever wanted to add a link or two at the end of a row in a model's change list? Say you had a model for people and a model for registrations or blog posts and you want to modify the people change list so it has a link to, say all of the registrations or blog posts for the person.
Well, Django provides half of the solution already in that the example registration change_list already handles the display of all registrations tied to that person. For example, the url `/admin/registrations/registration/?person__id__exact=121` gets you to a filtered list of registrations for the person with the id of 121. This is the same url used if you use list_filter in your model definition (though setting list_filter is not required for what we're doing).
Okay, so to add a link to the end of each person row in the change list, you need to create a template tag similar to "person_result_list" in the code. There, I've given an example that adds two links. Each dictionary in additional_links needs to have at least a text, sortable, and url_template attribute. The text attribute is what will display as the header to the column. url_template will be fed the id of the row object (in this example, a person id), which you can use to construct the link for each row. You could extend this if you wish, though all I ever need is the id.
And the last step is to use your new template tag in a modified change_list.html in place of the default result_list tag. The example at the bottom of the code shows an example usage of the tag.
Hope this makes sense and is helpful to someone!
UUIDField is a field which stores an uuid generated by pythons new uuid module.
it's included with the python 2.5 distribution by default, but if you run an older version of python you can happily copy the file from 2.5 to django/utils/uuid.py or your project directory.
Example (in project/application/models.py):
register_custom_permissions_simple((("is_editor", "User is editor"),))
In a view:
if not request.user.has_perm('application.is_editor'):
return HttpResonseRedirect(LoginUrl)
A simple login form that does the actual authentification itself.
**Usage:**
if request.method == "POST":
loginform = LoginForm(request.POST)
if loginform.login():
return HttpResponseRedirect(redir_url)
else:
loginform = LoginForm()
This is an extendend version of the Rails Flash implementation by Sean Patrick Hogan that supports different message types.
**Setting a flash message:**
request.flash.error = 'Item could not be saved'
request.flash['error'] = 'Item could not be saved'
request.flash['foo'] = 'bar'
**Displaying a flash in the view:**
<!-- show the error message -->
{% if flash.error %}An error occured:{{ flash.error }}{% endif %}
<!-- just show the first message found -->
{% if flash %}An error occured:{{ flash }}{% endif %}
<!-- show all messages -->
{% for msg in flash %}{{ msg.type }}: {{ msg.msg }}{% endfor %}
Note that it still works with simple strings as well. Feel free to just use it like this:
request.flash = "Message"
And:
{% if flash %}{{ flash }}{% endif %}
However, be aware that once you did this, you destroyed the Flash() dict and thus lost the extended functionality.
You can use request.flash.clear() to remove all messages.
Deprecated. I don't use this any more.
Hi,
I want decimal input which uses a comma as decimal seperator. It was quite complicated,
but it works.
It can be used as an example how to create an own subclass of an existing db.Field class and how to pass the dbfield to the widget, and use it in its render() method.
I think my snippet is too complicated, but couldn't find a better solution. If you do, please tell me.
Some time you want to add some common fields to a group of models, for example, in a **Generalization/Specialization** relationship. One could have a base model as the generalization class and specialized models with a foreign key to that base model with an unique attribute but I don't like it that way so, I just do this code to add some commons attributes to a lot of models.
If you have many models that all share the same fields, this might be an option. The fields are added directly to each model, e.g. while they will be duplicated on the database level, you only have to define them once in your **python** code.
This code is a cleaner way(I think!!!) to do it and will do the same that [this one](http://www.djangosnippets.org/snippets/317/). I hope this piece of code will be useful for you.
Use this script to import the Maxmind GeoIP lite CSV datasets into your database. This takes at least 200MB of RAM; the resulting database will be ~400MB. Stick in the same directory as the [models](http://www.djangosnippets.org/snippets/327/). Make sure to set `DEBUG=False` to prevent running out of memory during import.
This provides GeoDjango models for the maxmind GeoIP Lite data products. Use the corresponding [CSV import script](http://www.djangosnippets.org/snippets/328/) for data import. Requires: [GeoDjango](http://code.djangoproject.com/wiki/GeoDjango) and the [BigIntegerField patch](http://code.djangoproject.com/attachment/ticket/399/django-bigint-20070712.patch) by Peter Nixon.
I wanted to make the objects of a particular model approvable and store the timestamp of when that happened. In other frameworks/languages, I used to combined those in one "approved_at" field, which would be NULL if an object was currently unapproved.
I tried different approaches to implement this in django, and this is the best I came up with so far. Basically, the code in __setattr__ makes sure that the field, once set, will not be updated again.
Overriding setattr__() could also be a solution to determining if a field value has changed in save(), a question that seems come up from time to time in #django.
Can be used if a form field should not be editable, but the current value or the value that will be automatically used should still be visible to the user.
__init__ takes two additional parameters: **value** is the actual value to be used when saving the form, while **display** determines what is shown to the user when rendering. If *display* is not specified, *value* itself will be used instead.
If *display* is a *ModelChoiceField*, *value* is assumed to be a primary key of the model, and the widget will automatically try to retrieve and use the string representation of the corresponding item.