Opened 3 years ago

Closed 3 years ago

#19234 closed New feature (needsinfo)

can't adapt type error with models

Reported by:… Owned by: nobody
Component: Database layer (models, ORM) Version: 1.4
Severity: Normal Keywords: pickle, celery
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Sometimes with Django you want to perform operations which need to pickle model objects, you end up with a can't adapt type error. This is often true when one uses celery, for example.
I have a simple way to resolve this issue for model classes, but I wonder if it's relevant to add this code to Django itself.
This would be a new function in the django.db.models.base.Model class

def __reduce__(instance):
    names = [ for f in instance._meta.fields]
    attributes = dict([(name, getattr(instance, name)) for name in names])
    return (instance.__class__, (), attributes)

Worth adding or not ?

Change History (1)

comment:1 Changed 3 years ago by russellm

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to needsinfo
  • Status changed from new to closed

Words like "sometimes" aren't very helpful in a bug report.

Django models *should* be serializable already. If you actually look at the code for django.db.models.base.Model, there is already an implementation of __reduce__, and it's a lot more complex than what you propose (for very good reasons).

If you're having problems pickling models, you'll need to provide a specific example that reproduces your problem.

Closing needinfo; feel free to reopen if you can provide a specific example.

Note: See TracTickets for help on using tickets.
Back to Top