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:
I hereby claim:
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): |
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) | |
""" |
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. |
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') |
""" | |
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. | |
""" |
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 |