Custom Field Default Value Serialization

10 views
Skip to first unread message

qilin qi

unread,
Mar 2, 2017, 3:27:34 PM3/2/17
to django...@googlegroups.com
Hi All,


Apologize if this question has been asked before. This is my first question into this mailing list. 

I am creating a custom field(MyCustomField) with a custom value class(MyValue). I do have a deconstruct() method for my field class. And I would like to have a default value of this field. 

class MyModel(models.Model):
    myValue = MyCustomField(default = MyValue(args))


But when I do the migration, error happens and I realized I need a deconstruct() method(or @deconstructible) my value class. (https://docs.djangoproject.com/en/1.10/topics/migrations/#custom-deconstruct-method) to serialize the default value. There is no error after I do that. But my question is,


Based on the guide, I am supposed to be able to write a custom field using existing value class which I don't have access to the source. Quote below,

"In order to use the Hand class in our models, we do not have to change this class at all. This is ideal, because it means you can easily write model support for existing classes where you cannot change the source code."


So is giving a deconstruct() method and probably a __eq__() method violate this principle? I know I can subclass the existing class. But is it what I supposed to do? I just want to make sure my understanding is correct and what is the right design rule here? 


Thanks 

--
Qilin Qi 

Reply all
Reply to author
Forward
0 new messages