Re: Pickling of dinamically created models.Model subclasses: "attribute lookup failed", django 1.5

233 views
Skip to first unread message

Greg H

unread,
Apr 18, 2013, 3:07:20 PM4/18/13
to django...@googlegroups.com
Running into a similar issue on my own project, seems to be when you try an cache a model which has a related model which in turn has a many to many field. So for example, I have an instance of a Student, which has a ForeignKey to Book, which in turn has a ManyToMany to Author. If I try and cache my Student instance, I get that pickling error. In Django 1.4 it worked fine, but not in 1.5.

Figure out a resolution to this?

On Tuesday, March 19, 2013 10:17:46 PM UTC-7, Sir Anthony wrote:
Hello,
I'm using django-audit-log witch actively creates dynamic models by calling type('%sAuditLogEntry' % meta_name, (models.Model,), attrs). In django 1.5 I getting error when trying to cache it (pickling phase). Example error string: Can't pickle <class 'testproject.models.TestItemAuditLogEntry'>: attribute lookup testproject.models.TestItemAuditLogEntry failed. I think it because pickle tries to use class name with current environment.
I looked in django sources and found that this commit is cause of such behaviour.
I'm not sure it is django bug (I'll write a ticket with test case and probably patch, instead), maybe I just need to made some hacks on my side, like implement intermediate model with custom __reduce__ like in 1.4 django or something alike.

akaariai

unread,
Apr 18, 2013, 7:06:20 PM4/18/13
to Django users
On 18 huhti, 22:07, Greg H <ghi...@gmail.com> wrote:
> Running into a similar issue on my own project, seems to be when you try an
> cache a model which has a related model which in turn has a many to many
> field. So for example, I have an instance of a Student, which has a
> ForeignKey to Book, which in turn has a ManyToMany to Author. If I try and
> cache my Student instance, I get that pickling error. In Django 1.4 it
> worked fine, but not in 1.5.
>
> Figure out a resolution to this?

The problem is that for an instance of a class to be picklable the
class must be available as a module level attribute somewhere. For
dynamically created classes this is usually not true - they aren't
available at module level. One set of such classes is automatically
created through models for m2m fields. The automatic through classes
could explain the ManyToMany problem.

All in all this looks like a regression that should be fixed in 1.5. I
created a ticket for this, see: https://code.djangoproject.com/ticket/20289

- Anssi

akaariai

unread,
May 21, 2013, 11:31:20 AM5/21/13
to Django users
I have finally found the time to work on this issue. There is a patch
in the ticket (https://code.djangoproject.com/ticket/20289), and I
think the patch does solve the regression.

I didn't find a simple test case that works in 1.4 but fails in 1.5.
If anybody knows how to reproduce the original regression that would
be very useful in ensuring the regression is really solved.

- Anssi
Reply all
Reply to author
Forward
0 new messages