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.
What the docstring says. To not use some functionality, e.g. managing the value in the User's Profile model, delete the corresponding lines (when getting the page_size and when saving it.
Add the Mixin before the View class. e.g.: `class ItemList(PaginationMixin, generic.ListView):`
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.
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),
]`
## 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.
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.
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!
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`
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
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
### 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', ))