Home > Enterprise >  How can I get the rows from the PostgreSQL with specific value. Django
How can I get the rows from the PostgreSQL with specific value. Django

Time:09-20

How can I get the rows from the PostgreSQL with specific value? I have this table with columns: id | accountid | dictdata | date | Inside the dictdata column contains a bunch of dictionary data.

in my models.py

class charts(models.Model):
    id = models.AutoField(primary_key=True)
    accoundid= models.IntegerField(default=None,blank=True)
    dictdata=  models.JSONField(default=None,null=True, blank=True)
    date = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = "charts"

in my python file

  data = list(charts.objects.values('dictdata ')) //all dictionary data
  print(data)

Example of print(data):

   [{'dictdata': {'select': 'bar', 'title': 'some title'}},
   {'dictdata': {'select': 'line', 'title': 'some title'}},
  {'dictdata': {'select': 'pie', 'title': 'some title'}}]

how can I get the rows that contains a specific 'select' value? for example I want to get all the rows with 'bar'

{'dictdata': {'select': 'bar', 'title': 'some title'}}

CodePudding user response:

If the data inside dictdata is a dict and is not a list, you can filter like this:

Charts.objects.filter(dictdata__select="bar")

CodePudding user response:

You can get the rows from the PostgreSQL with specific value.

charts.objects.values('author').distinct()
  • Related