[Fixed]-Django custom field: when, exactly, do I have to write a deconstructor?


I think a better example of adding an extra option is CommaSepField in the documentation. It adds a separator argument, so deconstruct method has to be overridden to add it to the kwargs.

In the case of HandField, the max_length is always 104. Therefore there’s no point in the deconstruct method including max_length in kwargs, since it’s hardcoded. Overriding the deconstruct method and removing max_length makes it tidier.

As I understand it, the documentation says that overriding deconstruct for the HandField is optional. It is possible to do HandField(max_length=104), but it’s unnecessary, since the value will be overridden in __init__. However, it is necessary to add separator to the kwargs in CommaSepField, otherwise it won’t be possible to reconstruct the field.

Leave a comment