Answer the question
In order to leave comments, you need to log in
How to make a second request in annotate?
Good afternoon, there was a need to add a field to the output of a get request
def annotate_poll_name(self, obj):
steps = Step.objects.filter(block=obj).annotate(
poll_text=Poll.objects.get(id=447).text
)
return StepSerializer(steps, many=True).data
class Step(models.Model):
STEP_TYPE = (
('poll', 'Опрос'),
('questions', 'Вопросы'),
('file', 'Файл')
)
type = models.CharField(
max_length=60,
choices=STEP_TYPE
)
object_id = models.IntegerField(null=True)
block = models.ForeignKey(Block, on_delete=models.CASCADE, related_name='steps')
sort_order = models.PositiveIntegerField(default=1, validators=[MinValueValidator(1)])
Answer the question
In order to leave comments, you need to log in
Ebushki are sparrows. Instead of object_id, make the poll = models.ForeignKey(Poll, ***) field, now you can access the associated object as you like, you can make a property and it will automatically fall into the serializer if it is a subclass of ModelSerializer (but this is not accurate, you need to check)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question