Django sweet utils.
A little django code sugar.
If you find this package useful, please star it on GitHub.
Quickstart
-
Add
django_sweet_utilsto yourINSTALLED_APPSsetting 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:
uuid4as object id;created_atas object creation time;updated_atas object last update time;is_deletedas indicator that object is deleted or not;
Models that inherited from
django_sweet_utils.db.models.Modelcan 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=FalseAlso, now you don't need to catch
DoesNotExisterror when attempting to get some object while it does not exist. Just useget_or_none()instead ofget()and query returnsNoneif 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)
Metadata
Release files for django-sweet-utils 1.5.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django-sweet-utils-1.5.0.tar.gz | 11.6 kB | Without provenance | Details for django-sweet-utils-1.5.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_sweet_utils-1.5.0-py3-none-any.whl | Python 3 | none | any | Details for django_sweet_utils-1.5.0-py3-none-any.whl |
Total release size: 24.2 kB
Release files / django-sweet-utils-1.5.0.tar.gz
| Download URL | django-sweet-utils-1.5.0.tar.gz |
|---|---|
| Size | 11.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fc8a67ed886e2aaed06c8f2edfbab242f04cf6cd6af2421b73f11ea8a7eabfb2
|
|
BLAKE2b-256 checksum How to use checksums |
e4666667c1fa156add7c1bb9d49a4175be663469a2060a301490fc5c116b77ab
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.11.0
|
Release files / django_sweet_utils-1.5.0-py3-none-any.whl
| Download URL | django_sweet_utils-1.5.0-py3-none-any.whl |
|---|---|
| Size | 12.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
42beace9fef6bc47f91f5615cba0bb33b953e11c2cd489a7118e1f6157d84aeb
|
|
BLAKE2b-256 checksum How to use checksums |
9de82562a4df30a4444d45572f8bc53cb7bcb1e15c534a8420941dadc95df56f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.11.0
|
Security for django-sweet-utils 1.5.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.