Django Annoying
django-annoying is a Django application designed to simplify common, repetitive tasks and eliminate boilerplate code in Django projects. It provides a collection of useful decorators for views (like `@render_to`, `@ajax_request`, `@autostrip`), utility functions (`get_object_or_None`, `get_config`), and custom model fields (`JSONField`, `AutoOneToOneField`). The library is currently at version 0.10.8, with recent updates adding support for newer Django versions and deprecating features now built into Django itself. It maintains an active release cadence, typically addressing compatibility and minor feature enhancements.
Common errors
-
TypeError: 'dict' object is not callable when using JSONField default
cause You've set a mutable dictionary directly as the `default` argument for `JSONField` (e.g., `JSONField(default={})`), leading to a shared default object and potential runtime errors when the system tries to call it as a callable.fixProvide a callable for the default, like `JSONField(default=dict)`. This ensures a fresh dictionary is created for each new model instance. -
Http404 is not caught or handled when using @render_to decorator
cause When using `@render_to`, the decorator processes the return value of the view. If `Http404` is raised *after* the decorator's execution flow might have started or expects a dictionary, it can interfere with standard Django exception handling.fixEnsure `Http404` is raised before the decorator attempts to process a return value, typically at the beginning of the view or within a `get_object_or_404` call. Alternatively, if a non-dictionary (like an `HttpResponseRedirect` or a direct `HttpResponseNotFound`) is returned, `@render_to` will pass it through without processing.
Warnings
- deprecated The `annoying.decorators.signals` decorator is officially deprecated and will be removed in a future version. Django now includes native capabilities for signal handling, making this decorator redundant.
- deprecated The `annoying.decorators.autostrip` decorator is deprecated. Modern Django forms (CharField and Textarea) provide native stripping functionality by default (via `strip=True`).
- gotcha Using a mutable object (like `dict` or `list`) directly as the `default` argument for `annoying.fields.JSONField` (or any Django field) will cause all model instances to share the *same* default object. Modifying it for one instance will affect all others.
- breaking As of recent versions, `django-annoying` has dropped support for unmaintained and unsupported versions of Django. It explicitly requires Django 1.11 or later.
Install
-
pip install django-annoying
Imports
- render_to
from annoying.decorators import render_to
- ajax_request
from annoying.decorators import ajax_request
- JSONField
from annoying.fields import JSONField
- AutoOneToOneField
from annoying.fields import AutoOneToOneField
- get_object_or_None
from annoying.functions import get_object_or_None
- signals
from django.db.models import signals
from annoying.decorators import signals
- autostrip
from django import forms
from annoying.decorators import autostrip
Quickstart
from django.shortcuts import render
from django.http import HttpResponse
from annoying.decorators import render_to
# views.py example
@render_to('my_app/my_template.html')
def my_view(request):
context = {'message': 'Hello from django-annoying!'}
return context # The decorator handles rendering this dict to a template
# models.py example
from django.db import models
from annoying.fields import JSONField
class MyModel(models.Model):
data = JSONField(default=dict) # Use a callable for mutable defaults
def __str__(self):
return f"MyModel object with data: {self.data}"