Je veux afficher le nombre total de travailleurs associés à un compteur de tickets dans un sérialiseur parent (compteur de tickets).
Voici les deux Serializers :
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',
)
MODÈLES :
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')
Voici comment j'obtiens le résultat :
{
"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,
},
]
}
Voici comment je veux
{
"ticket_counter_name": "First",
"ticket_counter_description": "firsty",
"ticket_counter_address": "222fdssssss",
"workers": 2 # just the total count
}
Comment afficher uniquement le nombre total de personnes ?