How to formulate a query over two models?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

How to formulate a query over two models?

Mark Phillips
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 [hidden email].
To post to this group, send email to [hidden email].
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/CAEqej2MHC2BzZe3ptP7fgwAFB-SvuH7ffBTZfZLe%2BVjxQBvQ5A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to formulate a query over two models?

Christophe Pettus-2

> On Apr 11, 2018, at 16:05, Mark Phillips <[hidden email]> wrote:
>
> I have two models:
>
> # MetaData
> # MetaData Value

First, you might want to make sure this is *really* the best way of representing your data:

        http://karwin.blogspot.com/2009/05/eav-fail.html

That being said, you might consider getting rid of the MetaDataValue table, and putting the values in as a JSON field on MetaData.

--
-- Christophe Pettus
   [hidden email]

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/8B07A737-9B74-4389-8423-A6CD3C7BCA58%40thebuild.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to formulate a query over two models?

Mark Phillips
Thanks for the link - I have read it before. I need to stay with these models, so is there a simple query to get my result, or do I have to make multiple queries and combine the data in python?

Thanks,

Mark

On Wed, Apr 11, 2018 at 4:14 PM, Christophe Pettus <[hidden email]> wrote:

> On Apr 11, 2018, at 16:05, Mark Phillips <[hidden email]> wrote:
>
> I have two models:
>
> # MetaData
> # MetaData Value

First, you might want to make sure this is *really* the best way of representing your data:

        http://karwin.blogspot.com/2009/05/eav-fail.html

That being said, you might consider getting rid of the MetaDataValue table, and putting the values in as a JSON field on MetaData.

--
-- Christophe Pettus
   [hidden email]

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/8B07A737-9B74-4389-8423-A6CD3C7BCA58%40thebuild.com.
For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/CAEqej2Mrc7skJGzL%3DM42Zjm5W0tAVoLuz8p%2BuCQQTCxm_vT-QQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to formulate a query over two models?

Christophe Pettus-2

> On Apr 11, 2018, at 16:19, Mark Phillips <[hidden email]> wrote:
>
> Thanks for the link - I have read it before. I need to stay with these models, so is there a simple query to get my result, or do I have to make multiple queries and combine the data in python?

If you are using PostgreSQL, you can do it as a raw SQL query; otherwise, you'll need to transform the data in Python.

--
-- Christophe Pettus
   [hidden email]

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/C504BED8-2CD3-4873-A1EB-E7AD00FA6353%40thebuild.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to formulate a query over two models?

charettes
In reply to this post by Mark Phillips
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 <a href="http://self.name" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fself.name\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNE3JOkg7Vn23FjpPTI8mfGhfDqO3A&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fself.name\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNE3JOkg7Vn23FjpPTI8mfGhfDqO3A&#39;;return true;">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 [hidden email].
To post to this group, send email to [hidden email].
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.