Я хочу отобразить общее количество рабочих, связанных с счетчиком билетов, в родительском (счетчике) сериализаторе.
Ниже приведены два сериализатора:
class TicketCounterSerializer(serializers.ModelSerializer):
workers = WorkerToCounterSerializer(many=True, read_only=True)
class Meta:
model = TicketCounter
fields = (
'ticket_counter_name',
'ticket_counter_description',
'ticket_counter_address',
'workers',
)
class WorkerToCounterSerializer(serializers.ModelSerializer):
class Meta:
model = WorkerToTicketCounter
fields = (
'user',
'ticket_counter',
'worker',
)
МОДЕЛЬ:
class TicketCounter(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
ticket_counter_name = models.CharField(max_length=100, default="")
ticket_counter_description = models.CharField(max_length=1500, default="")
ticket_counter_address = models.CharField(max_length=1500, default="")
class WorkerToTicketCounter(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
ticket_counter = models.ForeignKey(TicketCounter,related_name="workers")
worker = models.ForeignKey(User,related_name='worker_for_ticket_counter')
Вот как я получаю результат:
{
"ticket_counter_name": "First",
"ticket_counter_description": "firsty",
"ticket_counter_address": "222fdssssss",
"workers": [
{
"user": 1,
"ticket_counter": 3,
"worker": 4,
},
{
"user": 1,
"ticket_counter": 3,
"worker": 5,
},
]
}
Вот как я хочу
{
"ticket_counter_name": "First",
"ticket_counter_description": "firsty",
"ticket_counter_address": "222fdssssss",
"workers": 2 # just the total count
}
Как просто отобразить общее количество?