Skip to content

Instantly share code, notes, and snippets.

View gepatino's full-sized avatar

Gabriel Patiño gepatino

View GitHub Profile

Keybase proof

I hereby claim:

  • I am gepatino on github.
  • I am gepatino (https://keybase.io/gepatino) on keybase.
  • I have a public key ASCqZXhItpr6ikCQ4snnpFo9DUg7TJCMp7nRC6rRLC_TeAo

To claim this, I am signing this object:

0xC299BdFA4FDcA92204fcD56c2c3BE2C69644be29
from django.conf import settings
from django.db import model
class AuditedModel(models.Model):
created = models.DateTimeFIeld(auto_now_add=True)
created_by = models.ForeignKey(settings.AUTH_USER_MODEL)
updated = models.DateTimeField(auto_now=True)
updated_by = models.ForeignKey(settings.AUTH_USER_MODEL)
def save(self, *args, **kwargs):
@gepatino
gepatino / multiple_char_filter.py
Created June 8, 2017 19:02
Filter to allow multiple options in Char filters for DRF + django_filters
import django_filters
class MultipleCharFilter(django_filters.CharFilter):
"""
Allows multiple options in a comma separated list for Char fields.
Example:
- field=value # filter by a single value
- field=val1,val2 # Filter by val1 OR val2 (Django's 'in' lookup)
"""
@gepatino
gepatino / serializers.py
Created May 29, 2017 19:11
Serializer with `extra_fields` option: add fields from the query arguments
class DefinitionSerializer(UserRelatedSerializer):
extra_fields = ('unread_notifications_count',)
def __init__(self, *args, **kwargs):
"""
Redefine __init__ to add extra_fiels feature.
You must add the field name to self.extra_fields, and add a method
`get_field_name` since internally this serialzier creates a
SerializerMethodField for each extra_field that is also in the
'extra_fields' query_param.
@gepatino
gepatino / serializers.py
Created May 29, 2017 18:26
Expandable Hyperlinked Related FIelds
from rest_framework import serializers
class ExpandableHyperlinkedRelatedField(serializers.HyperlinkedRelatedField):
"""
Field that returns a link to the related object by default, or the whole object document if the field name
is provided in the comma separated argument `expand_fields`.
"""
def __init__(self, *args, **kwargs):
self.expand_serializer = kwargs.pop('serializer')
@gepatino
gepatino / global_requests.py
Created May 19, 2017 13:22
Global Requests for Django
"""
This file contains a middleware and functions that makes possible to access
requests globally. This is very usefull when you need to check the current user
or some request headers in model's save() method, for example.
The middleware will store the current request in the _requests dictionary, so
you can use it later calling the get_current_requet or get_current_user
functions.
You just have to add the middleware to the MIDDLEWARE list (at the bottom is
ok), and the use the provided functions to access the request data.
"""
@gepatino
gepatino / models.py
Created May 11, 2017 17:58
Field change aware Django Model
from django.db import models
class StoredOriginalValuesModel(models.Model):
"""
Model that stores original data in a dictionary and provides the
field_has_changed(field_name) method to check if a field has been modified
after loading the data from the db.
Original values are stored in the _original dictionary only when creating
the object or calling refresh_from_db(), so the field_has_changed() method