Opened 9 years ago

Closed 4 years ago

Last modified 4 years ago

#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)

ticket_25370.tar.gz (2.4 KB ) - added by Mariusz Felisiak 4 years ago.
Sample project.

Download all attachments as: .zip

Change History (13)

comment:1 by Simon Charette, 9 years ago

Triage Stage: UnreviewedAccepted
Version: 1.8master

Makes sense to me, patch is welcome!

comment:2 by Tim Graham, 9 years ago

Has patch: unset

comment:3 by Adam, 9 years ago

Cc: awwester@… added
Owner: changed from nobody to Adam
Status: newassigned

comment:5 by Mariusz Felisiak, 4 years ago

Easy pickings: set
Owner: Adam removed
Status: assignednew

I think we can deassign after 5 years.

comment:6 by Tameesh Biswas, 4 years ago

Owner: set to Tameesh Biswas
Status: newassigned

Could I pick this up?

Version 0, edited 4 years ago by Tameesh Biswas (next)

comment:7 by Mariusz Felisiak, 4 years ago

Of course, there is no need to ask.

comment:8 by Ram Parameswaran, 4 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 Mariusz Felisiak, 4 years ago

Yes it's still valid, the same exception is raised in django/db/migrations/serializer.py.

comment:11 by Craig Smith, 4 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:12 by Mariusz Felisiak, 4 years ago

Owner: changed from Tameesh Biswas to Craig Smith
Patch needs improvement: set

by Mariusz Felisiak, 4 years ago

Attachment: ticket_25370.tar.gz added

Sample project.

comment:13 by Mariusz Felisiak, 4 years ago

Has patch: unset
Patch needs improvement: unset
Resolution: wontfix
Status: assignedclosed
Triage Stage: AcceptedUnreviewed

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.

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