Skip to main content

A little django code sugar.

Project description

Django sweet utils.

Downloads

A little django code sugar.

If you find this package useful, please star it on GitHub.

Quickstart

  1. Add django_sweet_utils to your INSTALLED_APPS setting like this:

    INSTALLED_APPS = [
        ...
        'django_sweet_utils',
        ...
    ]
    
  2. 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 simple existing() 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 use get_or_none() instead of get() and query returns None if there is no object.

Features

Models

Fake deletion

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.

UUID4 as object id

Every model instance has uuid4 field as object id.

Created and updated time

Every model instance has created_at and updated_at fields as object creation and last update time.

Existing objects

You can get only existing objects with existing() property on your model manager.

queryset = MyModel.objects.existing()

Get or none

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)

API

Views

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):
   ...

Pagination

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

Permissions

There is DjangoModelPermissions class that adds view permission to DjangoModelPermissions class on GET request method.

Admin

Hard deletion action for admin panel.

from django_sweet_utils.admin import hard_delete_selected

class MyModelAdmin(admin.ModelAdmin):
    actions = [hard_delete_selected]

Seriliazers

Prettier choice field

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)

Prettier multiple choice field

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)

Template tags

format_string template tag

You can use format_string template tag to format string with arguments.

{% load django_sweet_utils %}

{{ "Hello, {0}!".format_string("World") }}

set_query_string_param template tag

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.

Logging

Customised JSON formatter

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()

Customised JSON handler

There is CustomHandler class that handles log records as JSON.

from django_sweet_utils.logging import CustomHandler

handler = CustomHandler()

Misc

Custom JSON encoder

You can use LazyEncoder to serialize lazy objects to JSON.

from django_sweet_utils.misc import LazyEncoder

json.dumps({"lazy": lazy_object}, cls=LazyEncoder)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-sweet-utils-1.5.0.tar.gz (11.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_sweet_utils-1.5.0-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

Details for the file django-sweet-utils-1.5.0.tar.gz.

File metadata

  • Download URL: django-sweet-utils-1.5.0.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.0

File hashes

Hashes for django-sweet-utils-1.5.0.tar.gz
Algorithm Hash digest
SHA256 fc8a67ed886e2aaed06c8f2edfbab242f04cf6cd6af2421b73f11ea8a7eabfb2
MD5 42fd21c1d7daa426384ccb4cac9a906f
BLAKE2b-256 e4666667c1fa156add7c1bb9d49a4175be663469a2060a301490fc5c116b77ab

See more details on using hashes here.

File details

Details for the file django_sweet_utils-1.5.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_sweet_utils-1.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 42beace9fef6bc47f91f5615cba0bb33b953e11c2cd489a7118e1f6157d84aeb
MD5 f6e262cab748a95fea9d9290ad7286cb
BLAKE2b-256 9de82562a4df30a4444d45572f8bc53cb7bcb1e15c534a8420941dadc95df56f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page