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

JSONField not deserialized when used from inside a polymorphic inherited model #101

Closed
Karmak23 opened this issue Oct 26, 2014 · 11 comments · Fixed by #245
Closed

JSONField not deserialized when used from inside a polymorphic inherited model #101

Karmak23 opened this issue Oct 26, 2014 · 11 comments · Fixed by #245

Comments

@Karmak23
Copy link
Contributor

Hi,

I have models like this:

class BaseFeed(PolymorphicModel):
    errors = JSONField(default=list, blank=True)


class RssAtomFeed(BaseFeed):
    pass

Note: polymorphic is here.
Say I do:

f1 = BaseFeed.objects.create()
f2 = RssAtomFeed.objects.create()

Then I got:

f1.errors
[]

f2.errors
u'[]'

And this is pretty annoying because it will obviously fail in many places because all my code expects a list.

I have read #92, but I didn't find any obvious/easy clue on how to solve this issue.

Any other hint I can try?

For the record, my models are much complex than this. You can view the base feed here and the rss/atom feed here. I've tried with or without default, with or without load_kwargs={'object_pairs_hook': collections.OrderedDict}, this doesn't change anything.

I've tried the other JSONField implementation and the problem doesn't occur, even after a south migration (I'm on Django 1.6).

regards,

@Karmak23 Karmak23 changed the title JSONField not deserialized when used from inside a polymorphic model JSONField not deserialized when used from inside a polymorphic inherited model Oct 26, 2014
@jaredlewis
Copy link

I am having the same issue, has anyone come across any fixes for this?

@YamaoMaoen
Copy link

I guess this happens due to the pk check: https://github.com/bradjasper/django-jsonfield/blob/master/jsonfield/fields.py#L75

Because multi-table inherited objects have no pk, the field doesn't deserialize.
I would appreciate someone's explanation, why exactly it's needed.

@electroniceagle
Copy link

We're hitting this also, just came across this when trying to figure out why JSON strings are being passed to templates in a subclassed model. I think the pull request that brad didn't use here (https://github.com/mkhattab/django-jsonfield/commit/3583718f9ba6db510b555a5cb16338f0ee0095f9) might work. I'm going to fork and give it a try.

@gregbaker
Copy link

I think I am seeing the same issue. My code works (and has worked) under 0.9.20 but fails under 0.9.22 and later.

@bradjasper
Copy link
Collaborator

@electroniceagle were you ever able to try the forked version? I am trying to clear some time to work on this project and any additional info will help.

@electroniceagle
Copy link

I made a slightly different wrapping of the AttributeError try block and this is working for us in production, but I haven't tested whatever south data migration error you were attempting to fix with the pk if clause.

nimbis/django-jsonfield@bradjasper:master...master

vietord added a commit to cielo24/django-jsonfield that referenced this issue May 1, 2015
@nicolaslevy
Copy link

Thanks @vietord for that fix!

@vietord
Copy link

vietord commented Jul 21, 2015

@nicolaslevy All credit to @electroniceagle for the fix!

We have been running it in production for a while now, and we haven't run into any unexpected complications with it. (Django 1.4 environment)

However! We have not yet found ourselves in the scenario described by #52

None of the south(0.7.6) migrations we've done in areas which use JSONField since applying this fix do anything but simple db column addition or deletion manipulations.

@caffodian
Copy link

That fork fix seems to fix the inheritance case, but seems to break #33 again because it will try to parse ordinary strings if you pass them in as initial values. Poking around but can't find much documentation on the whole pk throwing an error because _ptr_id property doesn't exist that seems to be the cause of the original bug...

ubaumann added a commit to ubaumann/django-jsonfield that referenced this issue Jun 8, 2017
Fix polymorphic object issue: rpkilby#101
@youssefm
Copy link

youssefm commented Jan 4, 2018

Here's a class of test cases that should work, but don't if they're added to tests.py:

class SubclassedJsonModel(JsonModel):
    pass

class SubclassedJsonModelTest(JSONFieldTest):
    json_model = SubclassedJsonModel

Instead, I'm seeing 11 failures, most of which involve the deserialization not happening, for example:

AssertionError: u'"blah blah"' != 'blah blah'

bonzini added a commit to patchew-project/patchew that referenced this issue Jun 1, 2018
This fork is required to support multi-table inheritance.  The original has a bug
(rpkilby/jsonfield#101).

Signed-off-by: Paolo Bonzini <[email protected]>
bonzini added a commit to bonzini/patchew that referenced this issue Jun 7, 2018
This fork is required to support multi-table inheritance.  The original has a bug
(rpkilby/jsonfield#101).

Signed-off-by: Paolo Bonzini <[email protected]>
bonzini added a commit to patchew-project/patchew that referenced this issue Jun 29, 2018
This fork is required to support multi-table inheritance.  The original has a bug
(rpkilby/jsonfield#101).

Signed-off-by: Paolo Bonzini <[email protected]>
pvital pushed a commit to pvital/patchew that referenced this issue Oct 1, 2019
This fork is required to support multi-table inheritance.  The original has a bug
(rpkilby/jsonfield#101).

Signed-off-by: Paolo Bonzini <[email protected]>
@rpkilby
Copy link
Owner

rpkilby commented Feb 22, 2020

Hi all. Tests have added for Django's builtin multi-table inheritance, but not for integration with django-polymorphic. If there are still issues, please let me know. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.