Login

All snippets written in Python

Snippet List

get_object_or_none

This utility is useful when you want to safely retrieve a single object from the database without having to wrap get() in a try/except block every time. It also supports query optimization via select_related and prefetch_related, making it ideal for performance-conscious applications.

  • orm
  • queryset
  • utils
  • select_related
  • get_or_none
  • prefetch_related
Read More

Mask sensitive data from logger

This will help to secure the sensitive secrets, token, api keys, etc from logger. As we know there is security issue when we include the sensitive information to the logger in case logger got leaked/hacked. Before: ``` INFO ('192.168.1.1', 33321) - "WebSocket /ssh?token=abcdefg&width=20&heigh20" ``` After: ``` INFO ('192.168.1.1', 33321) - "WebSocket /ssh?token=********&width=20&heigh20" ```

  • django
  • security
  • logging
  • logger
Read More

Template tag - list punctuation for a list of items

If you have multiple items in a list and want them to be displayed as human readable list of items, this will add the proper punctuation to generate the text. You'll need to provide a conjugation to the end of the list like "or" or "and"; it defaults to "or". Intended use: `{% for item in items %}{{item}}{% list_punctuation forloop "and" %}{% endfor %}` * If items was `['a']`; the template would return `a`. * If items was `['a', 'b']`; the template would return `a and b`. * If items was `['a', 'b', 'c']`; the template would return `a, b, and c`.

Read More

Serializer factory with Django Rest Framework

Creates a model serializer class on the fly, just taking the model (class) as its argument. My use case: When importing data from spreadsheets, the DRF serializers are an easy way to create model instances from a dictionary. This function saves me from creating a custom serializer each time I add a new importer. Using `__all__` is dangerous ;-)

Read More

Help text hyperlinks

Sometimes a plain-text help-text isn't sufficient, and it's handy to be able to add links to pages, documentation or external websites. This javascript snippet can be added to your page, in combination with adding a class to your help text in your template. This assumes you're using jQuery on your website. Field template snippet: ``` {% if field.help_text %}<p class="help-text">{{ field.help_text }}</p>{% endif %} ``` On document ready, this will convert the markdown-style links into anchor tags, allowing you to have richer help text for your users

  • help
  • link
  • documentation
  • docs
  • href
  • help_text
Read More

Add custom fields to the built-in Group model

Add fields and extend Django's built-in `Group` model using a `OneToOneField` (i.e. a profile model). In this example, we add a `UUIDField`. Whenever a new group is created, we automatically (via signals) create a corresponding `Role` record referencing the newly created group. Whenever a Group is deleted, the corresponding Role is deleted as well.

  • model
  • group
  • uuid
  • signals
Read More

Month / Year SelectDateWidget based on django SelectDateWidget

A more simple version of [https://djangosnippets.org/snippets/1688/](https://djangosnippets.org/snippets/1688/), inheriting from `SelectDateWidget`, overriding only the necessarily. **Usage example:** **In models.py:** from django.db import models from django.utils.translation import gettext_lazy as _ class MyModel(models.Model): start = models.DateField( _("Start date"), ) end = models.DateField( _("End date"), ) class Meta: verbose_name = _("My model") **In forms.py:** from django import forms from .models import MyModel from .widgets import MonthYearWidget class MyModelForm(forms.ModelForm): class Meta: model = MyModel exclude = [] widgets = { "start": MonthYearWidget(), "end": MonthYearWidget(last_day=True), } **kwargs:** - *last_day :* if set to `True`, returns the last day of the month in the generated date.

  • widgets
  • select
  • year
  • month
  • SelectDateWidget
Read More

Python Django CRUD Example Tutorial

Hey Friends, In this quick example, let's see django tutorial & crud example with mysql and bootstrap. step by step explain django crud operations with mysql backend. step by step explain django crud operations with mysql bootstrap. let’s discuss about django crud operations with mysql database. Read more... [https://tuts-station.com/python-django-crud-example-tutorial.html](https://tuts-station.com/python-django-crud-example-tutorial.html)

  • django
  • python
  • crud
Read More

Browser-native date input field

Most modern browsers support the new `<input type="date">`, which allows inputting date using a browser-native date picker with built-in validation. This form widget utilizes this feature by setting the input type to "date" and by changing the value formatting as it should be in the ISO format. See more about `<input type="date">`: <https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date>

  • forms
  • datefield
  • widget
  • input
Read More

Generate and render HTML Table

A set of classes that enables fast and flexible generation of HTML tables from columns definitions and datasets. With classes arguments, it is easy to style it with bootstrap for example.

  • Table
  • HTML
  • Python rendering
Read More

2955 snippets posted so far.