I have a model that has a flags property, which is a bitmask of multiple values. I want to expose it as an API using django-rest-framework, where the different flags are different boolean properties. Say, if the flags are FLAG_NEW=1, FLAG_DELETED=2, I want to expose isNew and isDeleted fields. For read-only models, this is easy - just use a SerializerModelField and get whether the flag is set. However, that doesn't work when I want to deserialize (this is a read-only field). I could use a custom field, but then what should I put in the source= parameter? They will overwrite each other if I put source=flags and if I don't, then how do I get the initial value?
class MyModel(models.Model):
FLAG_NEW = 1
FLAG_DELETED = 2
flags = models.IntegerField()
....
class MyModelSerializer(models.Model):
isDeleted = ???
isNew = ???
BooleanFieldinstead of model atribute?