Django REST framework serializers: multiple fields mapping to one property
<p>I have a model that has a <code>flags</code> property, which is a bitmask of multiple values. I want to expose it as an API using <code>django-rest-framework</code>, where the different flags are different boolean properties. Say, if the flags are <code>FLAG_NEW=1</code>, <code>FLAG_DELETED=2</code>, I want to expose <code>isNew</code> and <code>isDeleted</code> fields. For read-only models, this is easy - just use a <code>SerializerModelField</code> 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 <code>source=</code> parameter? They will overwrite each other if I put <code>source=flags</code> and if I don't, then how do I get the initial value?</p>

<pre><code>class MyModel(models.Model):
    FLAG_NEW = 1
    FLAG_DELETED = 2

    flags = models.IntegerField()

....

class MyModelSerializer(models.Model):
    isDeleted = ???
    isNew = ???
</code></pre>