J
J
Jekson2021-08-26 16:25:13
Django
Jekson, 2021-08-26 16:25:13

Getting a list of field values ​​for a GenericForeignKey relationship?

There is a model associated with another via GenericForeignKey
models.py

class Report(models.Model):
    ....
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    usage_id = models.PositiveIntegerField(verbose_name='ID', db_column='id')
    usage = GenericForeignKey('content_type', 'usage_id')
    ....


Does djanga have a way/shortcut to get a list of usage field values ​​similar to
Models. objects.all().values_list('usage', flat=True)
only working with GenericForeignKey ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Antonio Solo, 2021-08-26
@Lepilov

`usage` is not a field, but a key
, I think that's how it should be
Report.objects.all().prefetch_related('usage')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question