Login

All snippets

Snippet List

Decorator to add placeholders to field

Decorator to automagically add placeholders to form widgets. `cls` can be any class derived from `django.forms.Form` or `django.forms.ModelForm`. The field labels are used as value for the placeholder. This will affect all form instances of this class. * add_placeholders only to forms.TextInput and form.Textarea * add_placeholders_to_any_field adds placeholders to any field Usage: @add_placeholders class Form(forms.Form): name = forms.CharField The name field will render as `<input type="text" placeholder="name">`

  • decorator
  • placeholder
Read More

django smartround filter

rounds floats in more human readable format e.g. 341.123434 mUSD -> 341mUSD 0.45345345 mUSD -> 0.5 mUSD 0.034545 mUSD -> 0.03 mUSD 0.0014545 mUSD -> 0.001 mUSD etc.

  • smart round
  • smartfloatformat
Read More

Custom DRF browsable API interface for InBBoxFilter of django-rest-framework-gis

[DRF browsable API interface](http://www.django-rest-framework.org/api-guide/filtering/#customizing-the-interface) for django-rest-framework-gis [InBBoxFilter](https://github.com/djangonauts/django-rest-framework-gis#inbboxfilter) Tested with Django==1.10.5 django-filter==1.0.1 djangorestframework==3.5.4 djangorestframework-gis==0.11 ![Screenshot](http://joxi.ru/8AnzxLDTOEgeAO.png)

  • django
  • django-rest-framework
  • django-rest-framework-gis
Read More

query builder

""" Takes arguments & constructs Qs for filter() We make sure we don't construct empty filters that would return too many results We return an empty dict if we have no filters so we can still return an empty response from the view """

  • query
  • queryset
Read More

aggregate filter

Makes it possible to add a filtering condition directly after the aggregate function (or possible, `aggregate(expression) WITHIN GROUP (ordering clause)`. This is mostly useful if the annotation has two or more expressions, so it's possible to compare the result with and without the applied filter; it's more compact than using `Case`. It's suggested to add `values` to the queryset to get a proper group by. Usage example: `books = Book.objects.values('publisher__name').annotate( count=Count('*'), filtercount=Filter(expression=Count('publisher__name'), condition=Q(rating__gte=5)) ) ` Supported on Postgresql 9.4+. Possible other third-party backends.

  • filter
  • postgresql
  • olap
  • aggregate
  • expression
Read More

replace

Small example of how to write your own function. This is not available in Django. The function just replaces static text strings, regular expressions are not supported. The syntax is the same in SQLite, PostgreSQL, MySQL and Oracle.

  • text
  • replace
Read More

CNPJ and CPF Validation for Models

The code was placed inside a helper file without using a class. The Django validator was not designed to work with validator classes, it would appear, so retrieving the value from the field proved to be a hassle. Just create a helper file, import it on your model, and use the validator in the standard way, as such: cnpj = models.CharField(unique=True, max_length=14, validators=[validate_CNPJ]) cpf = models.CharField(unique=True, max_length=14, validators=[validate_CPF])

  • model
  • validation
  • cnpj
  • cpf
Read More

Simply Gravatar Templatetags

Simply Gravatar Templatetags, for example the name of this templatetag is: `templatetags/gravatar_tags.py`, this supported for Python2 or Python3.

  • django
  • image
  • templatetag
  • gravatar
Read More

Django chunked queryset iterator

The function slices a queryset into smaller querysets containing chunk_size objects and then yield them. It is used to avoid memory error when processing huge queryset, and also database error due to that the database pulls whole table at once. Concurrent database modification wouldn't make some entries repeated or skipped in this process.

  • django
  • python
  • database
  • queryset
  • iterator
  • memoryerror
Read More

3109 snippets posted so far.