Skip to content

Commit

Permalink
Added __basefield__ attribute in VirtualField
Browse files Browse the repository at this point in the history
Keep track of the base model field from which the VirtualField has been created in the __basefield__ attribute.
  • Loading branch information
nemesifier committed Aug 25, 2014
1 parent 10c2eff commit 8268012
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
5 changes: 4 additions & 1 deletion django_hstore/virtual.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,14 @@ def create_hstore_virtual_field(field_cls, kwargs, hstore_field_name):
raise ValueError('field must be either a django standard field or a subclass of django.db.models.Field')

class VirtualField(HStoreVirtualMixin, BaseField):
# keep basefield info (added for django-rest-framework-hstore)
__basefield__ = BaseField

def __init__(self, *args, **kwargs):
try:
self.hstore_field_name = hstore_field_name
except KeyError:
raise ValueError('missing hstore_field_name keyword argument')
raise ValueError('missing hstore_field_name keyword argument')
super(VirtualField, self).__init__(*args, **kwargs)

def deconstruct(self, *args, **kwargs):
Expand Down
5 changes: 5 additions & 0 deletions tests/django_hstore_tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,11 @@ class FakeModel(object):
virtual_date = SchemaDataBag()._hstore_virtual_fields['date']

self.assertEqual(virtual_date.value_to_string(FakeModel()), '2014-08-08')

def test_basefield_attribute(self):
virtual_field = SchemaDataBag()._hstore_virtual_fields['char']
self.assertEqual(virtual_field.__basefield__.__name__, 'CharField')

else:
def test_improperly_configured(self):
with self.assertRaises(ImproperlyConfigured):
Expand Down

0 comments on commit 8268012

Please sign in to comment.