Ошибка атрибута во вложенном Serilizer
Я пытаюсь заполнить четыре модели в одном запросе Post с помощью Django Rest, где я получаю ошибку атрибута при возврате serializer.data.
Serializer.py
class TechnicianBasedTicketCreation(serializers.ModelSerializer):
technician_name = PrimaryKeyRelatedField(queryset=Technician.objects.all(), default=None)
ticket_timeslot_details = TicketTimeSlotSerializer(write_only=True)
service=TicketServiceSerializer(write_only=True)
class Meta:
model = Ticket
fields = ('technician_name','ticket_timeslot_details','service','ticket_type','latitude','longitude','address', )
def get_fields(self, *args, **kwargs):
fields = super(TechnicianBasedTicketCreation, self).get_fields()
qs = User.objects.filter(usertype__usertype='Technician',is_active=True)
fields['technician_name'].queryset = qs
return fields
def create(self, validated_data):
technician_name = validated_data.pop('technician_name')
service_obj = validated_data.pop('service')
time_slot_obj = validated_data.pop('ticket_timeslot_details')
pdb.set_trace()
ticket_created=Ticket.objects.create(ticket_type=validated_data['ticket_type'],latitude=validated_data['latitude'],longitude=validated_data['longitude'],address=validated_data['address'],current_status=TicketStatus.objects.get(status="Assigned"))
return ticket_created
Views.py
class TechnicianSelectTicketGeneration(APIView):
"""
"""
permission_classes = (permissions.IsAuthenticated, HasViewPermission)
required_scopes = ['groups']
serializer_class = TechnicianBasedTicketCreation
def post(self, request,*args, **kwargs):
serializer = self.serializer_class(data=request.data)
if serializer.is_valid():
serializer.save()
print(serializer.data)
return Response(serializer.data)
Я получаю сообщение об ошибке ниже, но мне нужно вернуть ответ данных сериализатора.
AttributeError в /api/v1/ticket/technician/ генерации билетов / объект 'Ticket' не имеет атрибута 'technician_name'
Мои модели
class Ticket(models.Model):
ticket_type=models.ForeignKey(TicketType, related_name='ticket_type',on_delete=models.CASCADE)
latitude=models.FloatField()
longitude=models.FloatField()
address=JSONField()
created_at =models.DateTimeField(default=timezone.now,null=True,blank=True)
is_deleted=models.BooleanField(default=False)
current_status=models.ForeignKey(TicketStatus,related_name='current_ticket_status',on_delete=models.CASCADE)
def __str__(self):
return u'%s' % (self.id)
class CurrentTicketStatus(models.Model):
ticket =models.ForeignKey(Ticket,on_delete=models.CASCADE,related_name='ticket_status_details')
ticket_status= models.ForeignKey(TicketStatus,on_delete=models.CASCADE)
user = models.ForeignKey(User,on_delete=models.CASCADE)
created_at=models.DateTimeField(default=timezone.now,null=True,blank=True)
def __str__(self):
return u'%s' % (self.id)
class TicketService(models.Model):
ticket = models.ForeignKey(Ticket,on_delete=models.CASCADE,related_name='ticket_service_details')
service=models.ForeignKey(Service,on_delete=models.CASCADE, related_name='ticket_service')
subservice=models.ForeignKey(SubService,on_delete=models.CASCADE, related_name='ticket_subservice')
issue_description=models.TextField()
booked_date=models.DateField(default=timezone.now,null=True,blank=True)
The PrimarykeyField Model is
class Technician(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE,related_name='technician_user')
vehicle=models.CharField(blank=True,null=True,max_length=150)
vehicle_no=models.CharField(blank=True,null=True,max_length=50)
license_no=models.CharField(blank=True,null=True,max_length=150)
def __str__(self):
return u'%s' % (self.id)
Пожалуйста, дайте мне знать, что я делаю не так.