
Product
Introducing Pull Request Stories to Help Security Teams Track Supply Chain Risks
Socket’s new Pull Request Stories give security teams clear visibility into dependency risks and outcomes across scanned pull requests.
A little django code sugar.
If you find this package useful, please star it on GitHub.
Add django_sweet_utils to your INSTALLED_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 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.
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
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)
format_string template tagYou 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 tagYou 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)
FAQs
A little django code sugar.
We found that django-sweet-utils demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Socket’s new Pull Request Stories give security teams clear visibility into dependency risks and outcomes across scanned pull requests.

Research
/Security News
npm author Qix’s account was compromised, with malicious versions of popular packages like chalk-template, color-convert, and strip-ansi published.

Research
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.