Serializers and ViewSets inheritance of PostgreSQL multitable models?
<p>My environment is based on Django 1.10 &amp; PostgreSQL 9.5 and;</p>

<p>I have a childtable which inherits from a parenttable.  For each, I have a Django model class, ChildModel and ParentModel respectively:</p>

<pre><code>class ParentModel(models.Model):
    id = models.AutoField(primary_key=True)
    owner = models.ForeignKey('auth.User', related_name='parents')

    class Meta:
        managed = False
        db_table = 'parenttable'


class ChildModel(ParentModel):
    name = models.CharField(unique=True, max_length=255)
    abbreviation = models.CharField(unique=True, max_length=2)

    class Meta:
        managed = False
        db_table = 'childtable'
</code></pre>

<p>Then, I made a serializer for each of these, and one for django.contrib.auth.models.User:</p>

<pre><code>class UserSerializer(serializers.HyperlinkedModelSerializer):
    parents = serializers.PrimaryKeyRelatedField(many=True, queryset=ParentModel.objects.all())

    class Meta:
        model = User
        fields = (['id', 'username', 'parents'])


class ParentModelSerializer(serializers.HyperlinkedModelSerializer):
    owner = serializers.IntegerField(source='parent.id', read_only=True, allow_null=False)

    class Meta:
        model = ParentModel
        fields = (['id', 'owner'])


class ChildModelSerializer(ParentModelSerializer):
    class Meta:
        model = ChildModel
        fields = (['name', 'abbreviation'])
</code></pre>

<p>Hence, I made three ViewSets, one for each serializer:</p>

<pre><code>class UserViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer


class ParentModelViewSet(viewsets.ModelViewSet):
    queryset = ParentModel.objects.all()
    serializer_class = ParentModelSerializer
    permission_classes = (permissions.IsAuthenticated, IsOwnerOrReadOnly)

    def perform_create(self, serializer):
        serializer.save(owner=self.request.user)


class ChildModelViewSet(ParentModelViewSet):
    queryset = ChildModel.objects.all()
    serializer_class = ChildModelSerializer
    permission_classes = (permissions.IsAuthenticated, IsOwnerOrReadOnly)
</code></pre>

<p>Is it possible to have inheritance from among serializers and viewsets?  I tried the former example, but every time I try to POST a new ChildModel, an exception is thrown showing that all ParentModel fields have NULL values (except for those from the ChildModel fields).</p>

<p>It isn't inheritance, but you can do this like:</p>

<pre><code>class ChildModelSerializer(ParentModelSerializer):
    class Meta:
        model = ChildModel
        fields = ParentModelSerializer.Meta.fields + ['name', 'abbreviation']
</code></pre>

<p>That's how I do it.</p>

python django postgresql django-models django-rest-framework