Skip to content

Bug with inherited models #129

Open
Open
@Stranger6667

Description

@Stranger6667

Hello!
I have following setup (based on django-hstore test setup):

class HStoreModel(models.Model):
    objects = hstore.HStoreManager()

    class Meta:
        abstract = True


class SerializedDataBag(HStoreModel):
    name = models.CharField(max_length=32)
    data = hstore.SerializedDictionaryField()


class InheritedSerializedDataBag(SerializedDataBag):
    extra_data = models.CharField(max_length=32)

I've tried to create & modify some data but get this:

>>> instance = InheritedSerializedDataBag.objects.create(extra_data='Test')
>>> instance.data = {'foo': 'bar'}
>>> instance.save()
>>> InheritedSerializedDataBag.objects.all()

Traceback (most recent call last):
  File "/Users/dmitrijdygalo/repos/django-hstore/tests/django_hstore_tests/tests/test_serialized_dictionary_field.py", line 469, in test_inherited_model
    instance = InheritedSerializedDataBag.objects.get(id=instance.id)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/manager.py", line 92, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py", line 351, in get
    num = len(clone)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py", line 122, in __len__
    self._fetch_all()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py", line 966, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py", line 275, in iterator
    obj = model(*row_data)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/base.py", line 382, in __init__
    setattr(self, field.attname, val)
  File "/Users/dmitrijdygalo/repos/django-hstore/django_hstore/descriptors.py", line 35, in __set__
    if value and self.field._from_db(obj):
  File "/Users/dmitrijdygalo/repos/django-hstore/django_hstore/fields.py", line 264, in _from_db
    return bool(model_instance._state.adding and model_instance.pk)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/base.py", line 509, in _get_pk_val
    return getattr(self, meta.pk.attname)
AttributeError: 'InheritedSerializedDataBag' object has no attribute 'serializeddatabag_ptr_id'

However in database data is not corrupted. Pointer to base table is OK, and data is also OK.
I use postgresql 9.4.1 and Django 1.7.9 during tests.
Here is test case for that. I'll try to find out why it happens, but I'm pretty new to codebase, so if anyone can help to fix this bug, it will be great :)

Cheers!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions