4

I am writing a simple database for the condo I live in which has a list of people, units, unit type (home vs parking space), and unitholder (join table for many-to-many relationship between a person and a unit) - one person can be the owner of a unit type of "home" while renting a parking space.

This is my model:

class Person(models.Model):
    first_name = models.CharField(max_length=30, null=False)
    last_name = models.CharField(max_length=30, null=False)
    phone = models.CharField(max_length=20)
    email = models.EmailField(max_length=20)

class UnitType(models.Model):
    description = models.CharField(max_length=30)

class Unit(models.Model):
    unit_number = models.IntegerField(null=False, unique=True)
    unit_type = models.ForeignKey(UnitType, null=False)
    unitholders = models.ManyToManyField(Person, through='UnitHolder')

class UnitHolderType(models.Model):
    description = models.CharField(max_length=30)

class UnitHolder(models.Model):
    person = models.ForeignKey(Person)
    unit = models.ForeignKey(Unit)
    unitholder_type = models.ForeignKey(UnitHolderType)

This is my view:

class PersonViewSet(viewsets.ModelViewSet):
    queryset = Person.objects.all()
    serializer_class = PersonSerializer

class UnitHolderTypeViewSet(viewsets.ModelViewSet):
    queryset = UnitHolderType.objects.all()
    serializer_class = UnitHolderTypeSerializer

class UnitViewSet(viewsets.ModelViewSet):
    queryset = Unit.objects.all()
    serializer_class = UnitSerializer

class UnitHolderViewSet(viewsets.ModelViewSet):
    queryset = UnitHolder.objects.all()
    serializer_class = UnitHolderSerializer

class UnitTypeViewSet(viewsets.ModelViewSet):
    queryset = UnitType.objects.all()
    serializer_class = UnitTypeSerializer

This is my serializer:

class UnitSerializer(serializers.ModelSerializer):
    unit_type = serializers.SlugRelatedField(
        queryset=UnitType.objects.all(), slug_field='description'
    )

    class Meta:
        model = Unit
        fields = ('unit_number', 'unit_type', 'unitholders')

class UnitTypeSerializer(serializers.ModelSerializer):

    class Meta:
        model = UnitType

class PersonSerializer(serializers.ModelSerializer):
    class Meta:
        model = Person

class UnitHolderSerializer(serializers.ModelSerializer):
    person = serializers.PrimaryKeyRelatedField(many=False, read_only=True)
    unit = serializers.PrimaryKeyRelatedField(many=False, read_only=True)

    class Meta:
        model = UnitHolder
        fields = ('person', 'unit', 'unitholder_type')

class UnitHolderTypeSerializer(serializers.ModelSerializer):
    class Meta:
        model = UnitHolderType

The problem:

When I query the /units endpoint like the following:

u = requests.get('http://localhost:8000/units').json()

My response looks like this:

[{'unit_type': 'Home', 'unit_number': 614, 'unitholders': [1]}]

What I want back is something like this:

[
    {
        'unit_type': 'Home', 
        'unit_number': 614, 
        'unitholders': [
            {
                'id: 1,
                'first_name': 'myfirstname',
                'last_name': 'mylastname',
                'unitholder_type': 'renter'
            }
        ]
    }
]

I'm pretty sure my problem is in my UnitSerializer but I am brand new to DRF and read the through the documentation but still can't seem to figure it out.

Progger
  • 2,266
  • 4
  • 27
  • 51

2 Answers2

4

An easy solution would be using depth option:

class UnitSerializer(serializers.ModelSerializer):
    unit_type = serializers.SlugRelatedField(
        queryset=UnitType.objects.all(), slug_field='description'
    )

    class Meta:
        model = Unit
        fields = ('unit_number', 'unit_type', 'unitholders')
        depth = 1

This will serialize all nested relations 1 level deep. If you want to have fine control over how each nested field gets serialized, you can list their serializers explicitly:

class UnitSerializer(serializers.ModelSerializer):
    unit_type = serializers.SlugRelatedField(
        queryset=UnitType.objects.all(), slug_field='description'
    )
    unitholders = UnitHolderSerializer(many=True)

    class Meta:
        model = Unit
        fields = ('unit_number', 'unit_type', 'unitholders')

Also as a side note, you need to look into modifying your querysets inside views to prefetch related objects, otherwise you will destroy the app performance very quickly (using something like django-debug-toolbar for monitoring generated queries is very convenient):

class UnitViewSet(viewsets.ModelViewSet):
    queryset = Unit.objects.all().select_related('unit_type').prefetch_related('unitholders')
    serializer_class = UnitSerializer
serg
  • 109,619
  • 77
  • 317
  • 330
  • thank you! this is all very helpful. depth = 1 worked for me but listing the serializer explicitly through errors. I will try to work those out myself :) – Progger Sep 26 '16 at 02:20
  • I just realized that "'unitholder_type': 'renter'" is not being returned. unitholder type is being held in a join table associating 'Unit' and 'Person'. I'd like to return not only the unit associated with the person, but also the association type which is held in the join itself. – Progger Oct 01 '16 at 16:38
0

Perhaps you must doing somethings so:

class UnitHolderViewSet(viewsets.ModelViewSet):
    queryset = UnitHolder.objects.all()
    unitholders = UnitHolderSerializer(read_only=True, many=True)

Django rest framework serializing many to many field

Community
  • 1
  • 1
  • hmm. thanks. I just added that to my view but it had no effect. querying that endpoint still returns: [{'unit_type': 'Home', 'unit_number': 614, 'unitholders': [1]}] – Progger Sep 25 '16 at 16:44