A little django code sugar.
If you find this package useful, please star it on GitHub.
-
Add
django_sweet_utils
to yourINSTALLED_APPS
setting like this:INSTALLED_APPS = [ ... 'django_sweet_utils', ... ]
-
Inherit your models from
django_sweet_utils.db.models.Model
:from django_sweet_utils.db.models import Model class MyModel(Model): ...
From now your models has the following fields:
uuid4
as object id;created_at
as object creation time;updated_at
as object last update time;is_deleted
as indicator that object is deleted or not;
Models that inherited from
django_sweet_utils.db.models.Model
can be filtered with simpleexisting()
property:from django_sweet_utils.db.models import Model class MyModel(Model): ... queryset = MyModel.objects.existing()
This returns queryset filtered by
is_deleted=False
Also, now you don't need to catch
DoesNotExist
error when attempting to get some object while it does not exist. Just useget_or_none()
instead ofget()
and query returnsNone
if there is no object.
You can delete your objects without actual database deletion.
Just use delete()
method on your model instance and it will be marked as deleted with is_deleted=True
:
To perform actual deletion use hard_delete()
method instead.
Every model instance has uuid4
field as object id.
Every model instance has created_at
and updated_at
fields as object creation and last update time.
You can get only existing objects with existing()
property on your model manager.
queryset = MyModel.objects.existing()
You can get object or None
if it does not exist with get_or_none()
method on your model manager.
obj = MyModel.objects.get_or_none(pk=1)
Inherit your DRF API views from django_sweet_utils.api.views
:
from django_sweet_utils.api.views import UpdateAPIView, DestroyAPIView
class MyUpdateView(UpdateAPIView):
...
class MyDestroyView(DestroyAPIView):
...
There is PageNumberPagination
class that adds page_size
query parameter to PageNumberPagination
class.
REST_FRAMEWORK = {
...
'DEFAULT_PAGINATION_CLASS': 'django_sweet_utils.api.pagination.PageNumberPagination',
'PAGE_SIZE': 10,
...
}
From now your views supports POST
request method instead of PATCH
and DELETE
DestroyAPIView does not perform actual database deletion, but only marks file as deleted with is_deleted=True
There is DjangoModelPermissions
class that adds view
permission to DjangoModelPermissions
class on GET
request method.
Hard deletion action for admin panel.
from django_sweet_utils.admin import hard_delete_selected
class MyModelAdmin(admin.ModelAdmin):
actions = [hard_delete_selected]
You can use custom ChoiceField
instead of ChoiceField
from rest_framework
to get prettier choices representation in API.
from django_sweet_utils.api.serializers import ChoiceField
class MySerializer(serializers.ModelSerializer):
my_field = ChoiceField(choices=MY_CHOICES)
You can use custom MultipleChoiceField
instead of MultipleChoiceField
from rest_framework
to get prettier choices representation in API.
from django_sweet_utils.api.serializers import MultipleChoiceField
class MySerializer(serializers.ModelSerializer):
my_field = MultipleChoiceField(choices=MY_CHOICES)
You can use format_string
template tag to format string with arguments.
{% load django_sweet_utils %}
{{ "Hello, {0}!".format_string("World") }}
You can use set_query_string_param
template tag to set query string parameter.
{% load django_sweet_utils %}
{% set_query_string_param "page" 1 %}
More info about this tag you can find here.
There is CustomisedJSONFormatter
class that formats log records as JSON.
from django_sweet_utils.logging import CustomisedJSONFormatter
APP_LABEL = "my_app"
ENVIRONMENT = "production"
formatter = CustomisedJSONFormatter()
There is CustomHandler
class that handles log records as JSON.
from django_sweet_utils.logging import CustomHandler
handler = CustomHandler()
You can use LazyEncoder
to serialize lazy objects to JSON.
from django_sweet_utils.misc import LazyEncoder
json.dumps({"lazy": lazy_object}, cls=LazyEncoder)