Skip to content

Commit

Permalink
docs update
Browse files Browse the repository at this point in the history
  • Loading branch information
harel committed May 31, 2018
1 parent 0204816 commit fb536b5
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions docs/index.rst
Expand Up @@ -199,20 +199,29 @@ In some cases it is useful to normalise those values, e.g., when wanting ot save
The default behaviour of using values as is can be changed by providing a ``normalise_function``
in your model. That function can evaluate the value's type and rewrite it accordingly.

::
This example shows the usage of the normalise function, with an extra paramter of a user's timezone
being passed as well:

::
import pytz
import datetime
from django.db import models
from dirtyfields import DirtyFieldsMixin

def your_normalise_function(value):
def your_normalise_function(value, timezone=None):
if isinstance(value, (datetime.datetime, datetime.date)):
return value.isoformat()
if timezone:
return pytz.timezone(timezone).localize(value).isoformat()
else:
return value.isoformat()
else:
return value

def get_user_timezone():
return 'Europe/London'

class TestModel(DirtyFieldsMixin, models.Model):
normalise_function = (your_normalise_function, {'extra_kwargs': 1})
normalise_function = (your_normalise_function, {'timezone': get_user_timezone()})


Why would you want this?
Expand Down

0 comments on commit fb536b5

Please sign in to comment.