Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for DurationField in schema mode #125

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion django_hstore/virtual.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from django.utils import six
from django.utils.functional import curry
from django import VERSION as DJANGO_VERSION
from datetime import timedelta
from django.utils.duration import duration_string

from .dict import HStoreDict

Expand Down Expand Up @@ -78,7 +80,13 @@ def __set__(self, instance, value):
setattr(instance, self.hstore_field_name, HStoreDict())
# reassign
hstore_dictionary = getattr(instance, self.hstore_field_name)
hstore_dictionary[self.name] = value

# Support DurationField
if isinstance(value, timedelta):
hstore_dictionary[self.name] = duration_string(value)

else:
hstore_dictionary[self.name] = value # default equivalent of str(value)

# end descriptor methods

Expand Down Expand Up @@ -155,6 +163,12 @@ def value_to_string(self, obj):
return val
VirtualField.value_to_string = value_to_string

# support DurationField
if BaseField == models.DurationField:
if (kwargs.get('null') or kwargs.get('blank')):
kwargs['default'] = None
VirtualField.value_to_string = duration_string

field = VirtualField(**kwargs)

if field.default == models.fields.NOT_PROVIDED:
Expand Down