Coder Social home page Coder Social logo

Comments (11)

charettes avatar charettes commented on September 28, 2024

Please provide a test case to reproduce.

from django-picklefield.

Koed00 avatar Koed00 commented on September 28, 2024

First think I found this morning is that v0.3.1 values_list('picklefield') would return undecoded strings. V0.3.2 decodes them. This breaks any code that tries to dbsafe_decode a values_list.

Running some more tests.

from django-picklefield.

Koed00 avatar Koed00 commented on September 28, 2024

The problem seems to be that in Django 1.8.4 , the values_list values are nice and decoded.
In Django 1.7.10 , the values_list values still return encoded .Which was the old behavior.

from django-picklefield.

Koed00 avatar Koed00 commented on September 28, 2024
testmodel = TestModel.objects.first()
testmodel.picklefield = 'hello'
TestModel.objects.filter(pk=testmodel.pk).values_list('picklefield', flat=True)[0]==testmodel.picklefield

In the current version , this is only true for Django 1.8.4 and not for Django 1.7.10
The fix for this used to be to dbsafe_decode the value, but since it's now already decoded with 1.8.4, dbsafe_decode raises an exception.

from django-picklefield.

charettes avatar charettes commented on September 28, 2024

As of Django 1.8+ you should expect all fields to be automatically decoded when using values(_list)?() as documented in the release notes.

You weren't getting any failures when using django-picklefield==0.3.1 because it was using the deprecated SubfieldBase API that was removed in 9cabb40.

Since this Django change is definitely an improvement and is here to stay I'll close this ticket.

from django-picklefield.

Koed00 avatar Koed00 commented on September 28, 2024

Do you have a suggestion how I run my code on both versions of Django without having to resort to checking for Django versions?

from django-picklefield.

charettes avatar charettes commented on September 28, 2024

I'm afraid this will really hard to achieve without relying on Django version checking. Can you point me to some code?

from django-picklefield.

Koed00 avatar Koed00 commented on September 28, 2024

I agree with you that this is a real improvement and I'm happy someone has taken it upon themselves to keep django-picklefield up to date.

    @staticmethod
    def get_result_group(group_id, failures=False):
        # values + decode is 10 times faster than just list comprehension
        if failures:
            values = Task.objects.filter(group=group_id).values_list('result', flat=True)
        else:
            values = Task.objects.filter(group=group_id).exclude(success=False).values_list('result', flat=True)
        return [dbsafe_decode(t) for t in values]

https://github.com/Koed00/django-q/blob/master/django_q/models.py

After the 0.3.2 update I removed the dbsafe_decode() iteration and that tested just fine. Unfortunately my projects tries to be 1.7 compatible too, so those tests then failed on Travis.
I've been thinking about refactoring a version of dbsafe_encode with a try clause in it, but that feels counterproductive. There is no real way of determining if a string is base64 encoded either.

from django-picklefield.

charettes avatar charettes commented on September 28, 2024

I'd would suggest you create a compat module with the following code and import it wherever you need to do some conditional decoding.

from picklefield import PickledObjectField, dbsafe_decode

if hasattr(PickledObjectField, 'from_db_value'):
    def decode_results(values):
        return values
else:
    def decode_results(values):
        return [dbsafe_decode(v) for v in values]

from django-picklefield.

Koed00 avatar Koed00 commented on September 28, 2024

Perfect, thanks a million.

from django-picklefield.

charettes avatar charettes commented on September 28, 2024

You could then replace your method by:

from .compat import decode_results
...
def get_result_group(group_id, failures=False):
    # values + decode is 10 times faster than just list comprehension
    if failures:
        values = Task.objects.filter(group=group_id).values_list('result', flat=True)
    else:
        values = Task.objects.filter(group=group_id).exclude(success=False).values_list('result', flat=True)
    return decode_results(values)

And simply remove the decode_results compatibly shim when dropping support for Django 1.7.

from django-picklefield.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.