#25370 closed Cleanup/optimization (wontfix)
Error display when makemigrations' field serializing fails with ValueError
| Reported by: | torstenrudolf | Owned by: | Craig Smith | 
|---|---|---|---|
| Component: | Migrations | Version: | dev | 
| Severity: | Normal | Keywords: | |
| Cc: | awwester@… | Triage Stage: | Unreviewed | 
| Has patch: | no | Needs documentation: | no | 
| Needs tests: | no | Patch needs improvement: | no | 
| Easy pickings: | yes | UI/UX: | no | 
Description
In django1.8.4 the error message displayed, when the field serialization fails (e.g. because there is a lambda function used for one argument) is not very helpful.
It would be nice to know on which field the error happened.
a simple patch could be inside django.db.migrations.writer to wrap line 377 (seems to be 422 in current master https://github.com/django/django/blob/e34226fc37dfa9eba89d913fd7ab8e95663b0d64/django/db/migrations/writer.py#L422) 
elif isinstance(value, models.Field): attr_name, path, args, kwargs = value.deconstruct() return cls.serialize_deconstructed(path, args, kwargs)
into a try-except block like this:
elif isinstance(value, models.Field): attr_name, path, args, kwargs = value.deconstruct() try: return cls.serialize_deconstructed(path, args, kwargs) except ValueError as e: e.args = ('During serialization of the field {} the following blew up: {}'.format(value, e.args[0]), ) + e.args[1:] raise e, None, sys.exc_info()[2]
end of the traceback before the change:
...
.../local/lib/python2.7/site-packages/django/db/migrations/writer.py", line 415, in serialize
    raise ValueError("Cannot serialize function: lambda")
ValueError: Cannot serialize function: lambda
after the change:
...
.../local/lib/python2.7/site-packages/django/db/migrations/writer.py", line 419, in serialize
    raise ValueError("Cannot serialize function: lambda")
ValueError: During serialization of the field <app_label>.<model_name>.<field_name> the following blew up: Cannot serialize function: lambda
      Attachments (1)
Change History (13)
comment:1 by , 10 years ago
| Triage Stage: | Unreviewed → Accepted | 
|---|---|
| Version: | 1.8 → master | 
comment:2 by , 10 years ago
| Has patch: | unset | 
|---|
comment:3 by , 10 years ago
| Cc: | added | 
|---|---|
| Owner: | changed from to | 
| Status: | new → assigned | 
comment:5 by , 5 years ago
| Easy pickings: | set | 
|---|---|
| Owner: | removed | 
| Status: | assigned → new | 
I think we can deassign after 5 years.
comment:8 by , 5 years ago
This module (django/db/migrations/serializer.py) was overhauled and merged in 2016 (see https://github.com/django/django/pull/6059).
Do you think a patch is still required?
comment:9 by , 5 years ago
Yes it's still valid, the same exception is raised in django/db/migrations/serializer.py.
comment:11 by , 5 years ago
| Has patch: | set | 
|---|
I didn't see any patch for this so I have added one with this PR .  Tameesh, may I reassign this?
comment:13 by , 5 years ago
| Has patch: | unset | 
|---|---|
| Patch needs improvement: | unset | 
| Resolution: | → wontfix | 
| Status: | assigned → closed | 
| Triage Stage: | Accepted → Unreviewed | 
It looks that it's not feasible to get <app label>.<model name>.<field name> or even <app label>.<model name> in a reliable way because Django serializes instances of fields from django.db.models not a model attributes. Each approach doesn't work in some cases, e.g. constructing messages in the FunctionTypeSerializer will not work for lambdas defined in the module
Error during serializing test_one.models.<lambda>: ...
or for lambdas imported from other modules:
ValueError: Error during serializing test_one.utils.<lambda>: ...
instead of ValueError: Error during serializing test_one.models.Model1.field1.
Makes sense to me, patch is welcome!