skip to Main Content

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'}}

2

Answers


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

    charts.objects.values('author').distinct()
    
    Login or Signup to reply.
  2. If the data inside dictdata is a dict and is not a list, you can filter like this:

    Charts.objects.filter(dictdata__select="bar")
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search