On Thu, Oct 10, 2013 at 6:02 PM, Daniel Roseman <dan...@roseman.org.uk> wrote:
> On Thursday, 10 October 2013 10:08:18 UTC+1, Marc Aymerich wrote:
>>
>> Hi Rafael, thanks !
>> I forgot to mention that I was already using django-jsonfield
>> https://github.com/bradjasper/django-jsonfield
>> and now I've tried with django-json-field but with the same unfortunate
>> result:
>>
>> TimeSerie.objects.filter(type='cpu').values('value')
>> [{'value':
>> '{"scheduled":2,"total":864,"15min":0.38,"1min":0.3,"5min":0.48}'},
>> {'value':
>> '{"scheduled":2,"total":859,"15min":0.34,"1min":0.23,"5min":0.32}'},
>> {'value':
>> '{"scheduled":2,"total":849,"15min":0.33,"1min":0.51,"5min":0.32}'}]
>>
>> did I missed something?
>>
>
> You need to show how you are storing this value in the first place.

yep, here we go

class TimeSerie(models.Model):
    type = models.CharField(max_length=64)
    value = JSONField()
    date = models.DateTimeField(auto_now_add=True)


# This is how the value is stored
value = json.loads(run(self.cmd, display=False).stdout)
TimeSerie.objects.create(type=self.name, value=value)


# And this is how it looks like using ValuesQuerySet vs accessing the
object value

>>> TimeSerie.objects.filter(pk=4769).values('value')
[{'value': 
'{"used":5358516,"cached":3191544,"free":2729444,"real-used":2044016,"shared":0,"real-free":6043944,"total":8087960,"buffers":122956}'}]

>>> TimeSerie.objects.filter(pk=4769)[0].values
{u'used': 5358516, u'cached': 3191544, u'free': 2729444, u'real-used':
2044016, u'shared': 0, u'real-free': 6043944, u'total': 8087960,
u'buffers': 122956}

One is an string and the other is a dict.

And here using raw sql
=# select value from monitor_timeserie where id=4769;
                                                                value
--------------------------------------------------------------------------------------------------------------------------------------
 
{"used":5358516,"cached":3191544,"free":2729444,"real-used":2044016,"shared":0,"real-free":6043944,"total":8087960,"buffers":122956}
(1 row)


it's a json field acctually
=# \d+ monitor_timeserie;
                                                      Table
"public.monitor_timeserie"
 Column |           Type           |
Modifiers                            | Storage  | Stats target |
Description
--------+--------------------------+----------------------------------------------------------------+----------+--------------+-------------
 id     | integer                  | not null default
nextval('monitor_timeserie_id_seq'::regclass) | plain    |
 |
 type   | character varying(64)    | not null
                             | extended |              |
 value  | json                     | not null
                             | extended |              |
 date   | timestamp with time zone | not null
                             | plain    |              |
Indexes:
    "monitor_timeserie_pkey" PRIMARY KEY, btree (id)
    "monitor_timeserie_date_4395efb9" btree (date, type)
Has OIDs: no


-- 
Marc

-- 
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2BDCN_urOzXqLBL6DfeVc3kTHQEz2xXsuiaP1Btr8S-ygAZFbg%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to