Hi, I am working with a Django rest api project. SO i need some help. I 
cant create a update for NESTED SERIALIZER. 
models.py

from django.db import models
from datetime import datetime
from django.contrib.auth.models import User


class TimesheetResponse(models.Model):
    date = models.DateField("Date")
    user = models.ForeignKey(User, default=1, on_delete=models.CASCADE)
    date_created = models.DateField(auto_now_add=True)
    date_last_updated = models.DateField(auto_now=True)

    def __str__(self):
        return '{} {}'.format(self.user, self.date, self.date_created, 
self.date_last_updated)


class SubjectType(models.Model):
    name = models.CharField(max_length=50)

    def __str__(self):
        return '{}'.format(self.name)


class Subject(models.Model):
    name = models.CharField(max_length=50)
    subject_type = models.ForeignKey(SubjectType, default=1, 
on_delete=models.CASCADE,
                                     related_name='subjects')

    def __str__(self):
        return '{} {}'.format(self.name, self.subject_type)


class TimesheetResponseUnit(models.Model):
    timesheet_response = models.ForeignKey(TimesheetResponse, default=1, 
on_delete=models.CASCADE,
                                           
related_name='timesheet_response_units')
    subject = models.ForeignKey(Subject, default=1, on_delete=models.CASCADE, 
related_name='timesheet_response_units')
    time_spent = models.IntegerField(default=0)
    comments = models.TextField(max_length=255)

    def __str__(self):
        return '{} {}'.format(self.subject, self.time_spent)



serializer.py 

from rest_framework import serializers
 
from timesheet.models import TimesheetResponse, TimesheetResponseUnit, 
SubjectType, Subject


class SubjectSerializer(serializers.ModelSerializer):
    class Meta:
        model = Subject
        fields = ['name', 'subject_type']


class SubjectTypeSerializer(serializers.ModelSerializer):
    class Meta:
        model = SubjectType
        fields = ['name']
        extra_kwargs = {'id': {'read_only': True}}


class TimesheetResponseUnitSerializer(serializers.ModelSerializer):
    class Meta:
        model = TimesheetResponseUnit

        fields = ['id', 'time_spent', 'comments', 'subject']
    # extra_kwargs = {'id': {'read_only': True}}


class TimesheetResponseSerializer(serializers.ModelSerializer):
    timesheet_response_units = TimesheetResponseUnitSerializer(many=True)

    class Meta:
        model = TimesheetResponse
        fields = ['date', 'user', 'timesheet_response_units']
        extra_kwargs = {'id': {'read_only': True}}

    def create(self, validated_data):
        timesheet_response_units = 
validated_data.pop('timesheet_response_units')
        timesheet_response = super(TimesheetResponseSerializer, 
self).create(validated_data)
        for timesheet_response_unit_data in timesheet_response_units:
            timesheet_response_unit = 
TimesheetResponseUnit(**timesheet_response_unit_data,
                                                            
timesheet_response=timesheet_response)
            timesheet_response_unit.save()
        return timesheet_response

     def update(self, instance, validated_data): ....


Any idea how to build a def update?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/4a7f8032-961a-4e84-9ebb-4464ff132aba%40googlegroups.com.

Reply via email to