Hello there,

If you are using PostgreSQL you can achieve it using ArrayAgg[0]

queryset = 
MetaData.objects.annotate(values=ArrayAgg('metadatavalue__value'))
map = dict(queryset.values_list('name', 'values'))

Cheers,
Simon

[0] https://docs.djangoproject.com/en/2.0/ref/contrib/postgres/aggregates/

Le mercredi 11 avril 2018 19:06:06 UTC-4, mark a écrit :
>
> I have two models:
>
> # MetaData
> class MetaData(models.Model):
>     metadata_id = models.AutoField(primary_key = True)
>     name = models.CharField('metadata name', max_length=200, unique=True)
>     description = models.TextField('description')
>
>     def __str__(self):
>         return self.name
>
> # MetaData Value
> class MetaDataValue(models.Model):
>     metadata_id = models.ForeignKey(MetaData, on_delete=models.CASCADE,)
>     value = models.CharField('value', max_length=200, unique=True)
>
>     def __str__(self):
>         return self.value
>
> I want to construct a query set such that I get a dictionary back that 
> looks like this:
>
> {name1: [value1, value2,..], name2:[value3, value4,..]..}
>
> In other words, a dictionary that has the metadata name as the key and the 
> associated values for that name in a list.
>
> I have been playing around with MetaData.objects.all(), but I don't see 
> how to relate the two models and get something close to what I want.
>
> Thanks!
>
> Mark
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/150f7ef8-2096-4724-8462-24c05c6de072%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to