William Zimmermann
William Zimmermann

Reputation: 916

Django Rest Framework - One to Many relationship not working


I'm trying to Serialize a Object that have Many Objects related. It's a Order has Many Logs relationship. I tried to do the same that in these tutorials (https://docs.djangoproject.com/en/2.1/topics/db/examples/many_to_one/ and http://www.django-rest-framework.org/api-guide/relations/#nested-relationships) but it doesn't work. I have also looked in various google link but nothing. Anyone can help me, please? What I'm doing wrong?

models.py

class MaintenanceOrder(models.Model):
    responsible_technician = models.ForeignKey(Technician, on_delete=models.DO_NOTHING, null=True)

    order_code = models.CharField(max_length=12, null=True)
    note_code = models.CharField(max_length=12, null=True)
    equipment = models.CharField(max_length=18, null=True)
    locale = models.CharField(max_length=30, null=True)
    short_description = models.CharField(max_length=40, null=False)
    description = models.TextField(null=True)
    priority = models.CharField(max_length=1, null=False)
    breakdown = models.CharField(max_length=1, null=True)
    stop_time = models.DateTimeField(null=True)
    end_time = models.DateTimeField(null=True)

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    status = models.ForeignKey(MaintenanceStatus, on_delete=models.DO_NOTHING)

    def __str__(self):
        return self.order_code

    class Meta:
        managed = True


class MaintenanceLog(models.Model):
    maintenance_order = models.ForeignKey(MaintenanceOrder, on_delete=models.DO_NOTHING)
    technician = models.ForeignKey(Technician, on_delete=models.DO_NOTHING, blank=True, null=True)

    start_time = models.DateTimeField()
    stop_time = models.DateTimeField(null=True)

    description = models.TextField(null=True)

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.description

    class Meta:
        managed = True

serializers.py

class MaintenanceLogSerializer(serializers.ModelSerializer):
    class Meta:
        model = MaintenanceLog
        fields = '__all__'


class MaintenanceOrderSerializer(serializers.ModelSerializer):
    status_code = serializers.IntegerField(source='status.status_code', read_only=True)
    order_log = MaintenanceLogSerializer(many=True, read_only=True)

    class Meta:
        model = MaintenanceOrder
        fields = '__all__'

This API it's returning all orders, but without "order_log"... Even if I specify "order_log" in fields, it doesn't return... Yes, there is related data in the database.

Additional Info
Python: 3.5
Django: 2.1

Upvotes: 0

Views: 858

Answers (1)

Enthusiast Martin
Enthusiast Martin

Reputation: 3091

order_log is not correct field of MaintenanceOrder model.

You need to specify source for order_log. In this case it is maintenancenlog_set.

Do this:

class MaintenanceOrderSerializer(serializers.ModelSerializer):
    status_code = serializers.IntegerField(source='status.status_code', read_only=True)
    order_log = MaintenanceLogSerializer(source='maintenancelog_set', many=True, read_only=True)

    class Meta:
        model = MaintenanceOrder
        fields = ( 'order_log', )

You need to explicitly specify it in the fields. Include all other MaintenanceOrder attributes.

Upvotes: 2

Related Questions