For historical reasons, Django commands are hard-coded to use `en-us` regardless of language. This includes the `shell` command and can lead to surprising effects if one assumes the local language to be the one set by `LANGUAGE_CODE`.
This snippets can be saved as a management command in any app, for example using the name `lshell` and provides a localized shell controlled by the normal `LANGUAGE_CODE` setting.
If you use django admin interface and have added an admin page for a model, django gives out-of-box search functionality in the model fields or foreignkey fields. One thing it doesn't support is searching in child models. For example you have created an admin page for Student model and there is model for courses which stores one or more courses taken by students and if you want to search by course name on the student page to see which students took a particular course. Django doesn't let you do that. I have written a small utility which will let you do that. Just copy the snippet in a file and then inherit from the ChildSearchAdmin instead of ModelAdmin and then you can specify which model/fields you want it to search on. The syntax is:
**child_searches = [(ChildModel, 'field_to_search_on', 'foreign_key_field_in_child_model'),..]
Example:
class StudentAdmin(ChildSearchAdmin):
child_searches = [(StudentCourse, 'course', 'student')]
Your MEDIA_ROOT directories are a mess? FileField save on "upload_to" directories with old/strange/temporary names decided "on the fly" and never fixed down?
SmartFolderFileField is the solution! "upload_to" directory depends only on: app, model and field names. No mess, no ambiguities
Obviously, in case you need a real callable for a dynamic directory name: please use it! and leave apart FixedFolderFileField
Sample:
from django.db import models
from utilities_app.models import SmartFolderFileField
class SampleModel(models.Model):
sample_char_field = models.CharField(max_length=50)
sample_file_field = SmartFolderFileField()
sterm is the search term. text is the result search text.
it will highlight every matched search term in search result. please define your own .yellow css class.
use:
{{search_result_var|highlight:search_term}}
The simplest way of displaying a "details" table about any model, is to show a ModelFrom with all fields readonly or (selects) disabled.
Also, the labels are preferably translatable, not just capitalized names of the column tables in your models. So the constructor translates the field labels as well.
This validator works well with FileField form fields and can validate that an uploaded file has an acceptable mimetype. Place this snippet in your app's `validators.py`.
Requirements:
This snippet uses [python-magic](https://github.com/ahupp/python-magic). To install:
pip install python-magic
Usage (in forms.py):
from validators import MimetypeValidator
class MyForm(forms.Form):
file = forms.FileField(
allow_empty_file=False,
validators=[MimetypeValidator('application/pdf')],
help_text="Upload a PDF file"
)
Copy this file into `your_app/serializer.py`, and add this to your settings:
SERIALIZATION_MODULES = {
'json': 'your_app.serializer',
}
Now you can dump your models with the classical `dumpdata -n` command and load it with `loaddata` and get support for natural primary keys and not only with foreign keys and many to many fields.
Because {% now "Y"|add:"-2005" %} - etc. doesn't work, you can use the above in your template like:
We have {{ 2000|since }} years of experience.
or:
Serving our customers with passion for more than {% years_since 2005 %} years.
Function that takes a bound form (submitted form) and returns a list of pairs of field label and user chosen value.
It takes care of:
1. fields that are not filled out
2. if you want to exclude some fields from the final list
3. ChoiceField (select or radio button)
4. MultipleChoiceField (multi-select or checkboxes)
Usage:
if form.is_valid():
form_data = get_form_display_data(form, exclude=['captcha'])
It's trivial to display the list of pairs in a template:
{% for key, value in form_data %}
{{ key }}: {{ value }}{% endfor %}
Provides UUIDField for your models. This version creates very short UUID represenation (21 chars) when the record is added eg. in admin. Generated ids are safe to be used in URLs.
You can put represent it in admin as
'readonly_fields=("uuid",)'
Django models IntegerField that get max_value and min_value in it's constructor and validate on it.
It's initialize the formfield with min_value and max_value, too.
Based on:
[View all log entries in the admin](https://djangosnippets.org/snippets/2484/)
Improvements:
* No crash on `object_link` when reverse route missing,
* Filter by users present in the log AND in database currently
* Other filters on users - only superusers / only staff (modify to fit your needs)
EDIT:
Incorporated `action_description` from [django-logentry-admin](https://github.com/yprez/django-logentry-admin).
* The list filter now has human-friendly action names, and the table also.
* Refactored list filter class hierarchy.
Despite warning coming from django developers, I'm still using admin classes to quickly get into reverse engineering databases.
One feature is missing: searching into fields thanks to a regex.
One dirty solution I found is to overwrite get_search_results. But most of the code comes from django itself.
If anyone has a better idea ;)
**Usage:**
1. works since get_search_results is part of ModelAdmin (1.5 if I remember well)
2. Inherit your Admin class from RegexModelAdmin
3. enclose by / the field you want to regex with:
`search_fields = ['/field/', ]`
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.