Note that there are some explanatory texts on larger screens.

plurals
  1. POdreaded "not the same object error" pickling a queryset.query object
    primarykey
    data
    text
    <p>I have a queryset that I need to pickle lazily and I am having some serious troubles. <code>cPickle.dumps(queryset.query)</code> throws the following error:</p> <pre><code>Can't pickle &lt;class 'myproject.myapp.models.myfile.QuerySet'&gt;: it's not the same object as myproject.myapp.models.myfile.QuerySet </code></pre> <p>Strangely (or perhaps not so strangely), I only get that error when I call <code>cPcikle</code> from another method or a view, but not when I call it from the command line.</p> <p>I made the method below after reading <a href="https://stackoverflow.com/questions/1412787/picklingerror-cant-pickle-class-decimal-decimal-its-not-the-same-object-a">PicklingError: Can&#39;t pickle &lt;class &#39;decimal.Decimal&#39;&gt;: it&#39;s not the same object as decimal.Decimal</a> and <a href="https://stackoverflow.com/questions/3292383/django-mod-wsgi-picklingerror-while-saving-object">Django mod_wsgi PicklingError while saving object</a>:</p> <pre><code>def dump_queryset(queryset, model): from segment.segmentengine.models.segment import QuerySet memo = {} new_queryset = deepcopy(queryset, memo) memo = {} new_query = deepcopy(new_queryset.query, memo) queryset = QuerySet(model=model, query=new_query) return cPickle.dumps(queryset.query) </code></pre> <p>As you can see, I am getting extremely desperate -- that method still yields the same error. Is there a known, non-hacky solution to this problem? </p> <p><b>EDIT:</b> Tried using <code>--noreload</code> running on the django development server, but to no avail.</p> <p><b>EDIT2:</b> I had a typo in the error I displayed above -- it was <code>models.QuerySet</code>, not <code>models.mymodel.QuerySet</code> that it was complaining about. There is another nuance here, which is that my models file is broken out into multiple modules, so the error is ACTUALLY:</p> <pre><code> Can't pickle &lt;class 'myproject.myapp.models.myfile.QuerySet'&gt;: it's not the same object as myproject.myapp.models.myfile.QuerySet </code></pre> <p>Where myfile is one of the modules under models. I have an <code>__ini__.py</code> in models with the following line:</p> <pre><code>from myfile import * </code></pre> <p>I wonder if this is contributing to my issue. Is there some way to change my <code>init</code> to protect myself against this? Are there any other tests to try?</p> <p><b>EDIT3:</b> Here is a little more background on my use case: I have a model called <code>Context</code> that I use to populate a UI element with instances of <code>mymodel</code>. The user can add/remove/manipulate the objects on the UI side, changing their context, and when they return, they can keep their changes, because the context serialized everything. A context has a generic foreign key to different types of filters/ways the user can manipulate the object, all of which must implement a few methods that the context uses to figure out what it should display. One such filter takes a queryset that can be passed in and displays all of the objects in that queryset. This provides a way to pass in arbitrary querysets that are produced elsewhere and have them displayed in the UI element. The model that uses the Context is hierarchical (using mptt for this), and the UI element makes a request to get children each time the user clicks around, we can then take the children and determine if they should be displayed based on whether or not they are included in the Context. Hope that helps!</p> <p><b>EDIT4:</b> I am able to dump an empty queryset, but as soon as I add anything of value, it fails.</p> <p><b>EDIT4:</b> I am on Django 1.2.3</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload