Login

All snippets written in Python

Snippet List

Smart wrapping Django admin's delete_selected

The idea here is to wrap the original `delete_selected` functionality in a way that I shouldn't have to reimplement the templates (confirmation/error response) serving, just extend the original. What this code does, it wraps the queryset's delete function with a closure, so when it really gets called (after the confirmation), it executes the extra functionality you wish to. After looking at the original code, this seemed to be the most efficient way of doing it.

  • admin
  • wrap
  • delete_selected
Read More

Make HTML5 appcache manifest slice work with django-compress compressed files

I needed to make appcache for my application which used django-compress for JS and CSS compression. This is, how I solved the problem with putting compressed files into the manifest. I went for offline compression (with `COMPRESS_OFFLINE=True`). This snippet shows code of command file (put it in `apps/cyklomapa/management/commands/compress_create_manifest.py`), which creates `compress_cache_manifest.txt` file in my templates. Then I just use `{% include "compress_cache_manifest.txt" %}` in my appcache template.

  • html5
  • django-compress
  • appcache
Read More

Mutiple forms on Form wizard

Wraps many Form subclases in order to get a form wizard to treat them as one. Many Forms will use one step. **Example:** `class LanguageVerifiedHumanForm(MultipleForms): base_forms = [ ('language_form', LanguageForm), ('verified_human_form', VerifiedHumanForm) ]` `class NewGuideWizard(SessionWizardView): form_list = [ ('guide_form', GuideForm), ('multi_form', LanguageVerifiedHumanForm), ]`

  • multiple
  • forms
  • wizard
  • multi
  • form-wizard
Read More

Filter changelist by a numeric field using a number of common value ranges

## How to use Use this [admin filter](https://docs.djangoproject.com/en/1.8/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter) together with a numeric field to allow filtering changlist by field values range (in this case, age groups): For example, to group customers by age groups: class Customer(models.Model): # ... age = models.IntegerField() age.list_lookup_range = ( (None, _('All')), ([0, 2], '0-2'), ([2, 4], '2-4'), ([4, 18], '4-18'), ([18, 65], '18-65'), ([65, None], '65+'), )) class CustomerAdmin(admin.ModelAdmin): list_filter = [('age', ValueRangeFilter), ] ## Inspiration [This snippet](https://djangosnippets.org/snippets/587/) (for django < 1.4) inspired me to make this work for newer django versions.

  • filter
  • admin
  • field
  • range
Read More

Dynamic formset without javascript

If using javascript is not an option, you can use something like this code to have a variable number of subforms. This code uses crispy-forms, but it is totally dispensable.

  • formset
  • dynamic-formset
  • formset_factory
Read More

Reusable field forms using crispy-forms

If you need to use some source to construct your form (maybe some database info or some user input), or you use the same fields with the same functionality in various forms and need to write the same piece of code for each of them, then you are looking in the right place. With this snippet you won't have those issues any more. :P This snippet present a way to wean the fields from the form. The code is made using crispy-forms, but the same idea can be applied without using it. Best luck!

  • forms
  • filters
  • reuse
  • reusable
  • form-field
  • crispy-forms
Read More

Django filter for shrinking [big] numbers

Simple filter that shrinks [big] numbers sufixing "M" for numbers bigger than million, or "K" for numbers bigger than thousand. It does a division over the number before converting to string so rounding is properly done. Examples: `{{ 123456|shrink_num }} >> 123.6K` `{{ 1234567|shrink_num }} >> 1.2M`

  • filter
  • numbers
  • kilo
  • mega
Read More

Datetime adjuster for Django Templates

The filter is **specific to datetime objects** and no allowance has been made to convert strings or epoch times. Also **no type checking** is performed so misuse will result in an error. To use include the above snippet in a file called templatetags/customfilters.py or append to existing filters file then load: `{% load customfilters %}` in your template, then to use it: `{{ dateobject|adjust:"months=1"|date:"Y/m/d" }}` To push the dateobject forward by a month, or: `{{ dateobject|adjust:"weeks=-1"|date:"Y/m/d" }}` To push the dateobject back a week, or: `{{ dateobject|adjust:"years=1, months=2"|date:"Y/m/d" }}` To push the dateobject forward by a year and 2 months

  • template
  • filter
  • datetime
  • calculation
  • adjust
  • relativedelta
Read More

Remove a clause from a queryset

I want to create Mixins for QuerySet objects that will by default filter out certain records (e.g. filter out "deleted" records, or filter out "unapproved" records, etc). I'd like those to be separate independent mixins. So in each of those, I override all() to filter out deleted or unapproved, etc. But, I also want to offer a method in the queryset to remove those filters or remove some part of those filters. That's where this code comes in. After some examination of how QuerySets work, this seemed like the simplest method for "undoing" some filter in a queryset

  • hack
  • orm
  • manager
  • mixin
  • queryset
Read More

ReadOnlyFieldsMixin to Form, ModelForm and Views (helper function)

### Usage: class Foo(models.Model): description = models.TextField() number = models.IntegerField() class FooOnlyDescriptionIsReadOnly(ReadOnlyFieldsMixin, forms.ModelForm): readonly_fields = ('description', ) class Meta: model = Foo fields = '__all__' class FooAllFieldsIsReadOnly(ReadOnlyFieldsMixin, forms.ModelForm): class Meta: model = Foo fields = '__all__' ### or use the function class FooForm(forms.ModelForm): class Meta: model = Foo fields = '__all__' ReadOnlyFooForm = new_readonly_form_class(FooForm, readonly_fields=('description', ))

  • readonly-form
  • ReadOnlyFieldsMixin
Read More

2955 snippets posted so far.