Django.fun

django-rest-framework ignores fields from model mixin

I have the following configuration of django models with base classes and a mixin for additional fields, but django-rest-framework is ignoring the fields from the mixin class.

Models

from django.db import models

class Base(models.Model):
  a = CharField()

  class Meta:
    abstract = True


class Mixin(object):
  b = CharField()


class Concrete(Base, Mixin):
  c = CharField()

  class Meta(Base.Meta):
    abstract = False

Serializers

from rest_framework import serializers

class BaseSerializer(serializers.Serializer):
  class Meta:
    fields = ('a',)
    abstract = True

class ConcreteSerializer(BaseSerializer):
  class Meta(BaseSerializer.Meta):
    model = Concrete
    fields = ('b', 'c',)

View sets

from rest_framework import viewsets

class BaseViewSet(viewsets.ModelViewSet):
  # Some common @actions here.

  class Meta:
    abstract = True

class ConcreteViewSet(BaseViewSet):
  serializer_class = ConcreteSerializer

  class Meta(EquipmentItemEditProposalViewSet.Meta):
    abstract = False

URLS

from rest_framework import routers

router = routers.DefaultRouter()
router.register(r'concrete', ConcreteViewSet, basename='concrete')

However, when I browese to http://localhost/api/concrete/ I get the django-rest-framework form with the a and c fields, but not the b field from the mixin.

NOTE: if I make the Mixin class inherig from django.db.models.Model instead of object, then I do get the field, but I that won't work because applying a django migration results in a diamond inheritance problem:

TypeError: Cannot create a consistent method resolution
order (MRO) for bases Model, Mixin

Is there a solution to this problem?

Thanks!

EDIT: what makes things work is if I explicitly add the fields to the Mixin to the BaseSerializer. However this is not as DRY as I'd like.

Answers: 0