Я хочу обновить поле сериализатора nester, ниже примера я хочу обновить поле teacher_date
Вот мой патч-вызов с данными
{
"user": {
"pk": 2,
"username": "rayees",
"email": "rayees.xxxx@xxxx.co.in",
"first_name": "Rayees",
"last_name": "xxxxx",
"user_type": "Teacher"
},
"first_name": "Rayees",
"last_name": "xxxx",
"bio": "TEST BIO",
"teacher_cost": "22.00",
"subject": [
"HINDI",
"ENGLISH"
],
"teacher_date": [
{
"day_available": "MON",
"time_available": "Morning"
},
{
"day_available": "SUN",
"time_available": "Morning"
}
],
"cities": [
"SANTA CLARA",
"SUNNYVALE"
]
}
В в этом примере все обновляется, кроме teacher_date
Model.py
class Availability(models.Model):
uid = models.AutoField(verbose_name='ID',
serialize=False,
auto_created=True,
primary_key=True)
MONDAY = 'MON'
TUESDAY = 'TUE'
WEDNESDAY = "WED"
THURSDAY = "THU"
FRIDAY = 'FRI'
SATURDAY = "SAT"
SUNDAY = "SUN"
DAY = (
(MONDAY, 'Monday'),
(TUESDAY, 'Tuesday'),
(WEDNESDAY, 'Wednesday'),
(THURSDAY, 'Thursday'),
(FRIDAY, 'Friday'),
(SATURDAY, 'Saturday'),
(SUNDAY, 'Sunday'),
)
day_available = models.CharField(
max_length=3, choices=DAY, default=MONDAY)
MORNING = 'Morning'
NOON = 'AfterNoon'
EVENING = 'Evening'
TIME = (
(MORNING, 'Morning'),
(NOON, 'Afternoon'),
(EVENING, 'Evening'),
)
time_available = models.CharField(
max_length=30, choices=TIME, default=MORNING)
def __str__(self):
return f"{self.day_available} {self.time_available}"
class Teacher(models.Model):
uid = models.AutoField(verbose_name='ID',
serialize=False,
auto_created=True,
primary_key=True)
user = models.OneToOneField(User, on_delete=models.CASCADE)
bio = models.TextField(null=True, blank=True)
teacher_cost = models.DecimalField(
max_digits=5, decimal_places=2, null=True, blank=True)
languages = models.CharField(
max_length=50, null=True, blank=True)
subject = models.ManyToManyField('Subject')
teacher_date = models.ManyToManyField(Availability)
cities = models.ManyToManyField('City', related_name='teacher')
def __str__(self):
return self.user.username
serializer.py
class AvailabilityDetails(serializers.ModelSerializer):
class Meta:
model = Availability
fields = ('day_available', 'time_available',)
class TeacherProfileDetails(serializers.ModelSerializer):
logger = logging.getLogger(__name__)
teacher_date = AvailabilityDetails(many=True, read_only=True)
first_name = serializers.CharField(source='user.first_name', read_only=True)
last_name = serializers.CharField(source='user.last_name', read_only=True)
cities = serializers.SlugRelatedField(many=True, slug_field='city', queryset=City.objects.all(),)
subject = serializers.SlugRelatedField(many=True, slug_field='subject', queryset=Subject.objects.all(),)
teacher_date = AvailabilityDetails(many=True)
user = UserDetailsSerializer(read_only=True)
class Meta:
model = Teacher
fields = ('user', 'first_name', 'last_name',
'bio', 'teacher_cost', 'subject', 'teacher_date', 'cities')
def update(self, instance, validated_data):
self.logger.info("-------- Inside the update --------------")
teacher_date = validated_data.pop('teacher_date')
self.logger.info(teacher_date)
teacher_date = instance
teacher_date.__dict__.update(validated_data)
teacher_date.save()
return teacher_date
Здесь я делаю некоторую ошибку в сериализации, для teacher_date, метод обновления вызывается во время операции исправления, но новые добавленные значения не обновляются в БД