Hello Django community,

I'm pretty new to Django and I'm getting stuck on the following issue. 

I have a model defined as follows :

class temp_db(models.Model):
date = models.DateTimeField()
tempext = models.DecimalField(max_digits=4, decimal_places=2)
tempeau = models.DecimalField(max_digits=4, decimal_places=2)
humid = models.DecimalField(max_digits=4, decimal_places=2)

def __str__(self):
return str(self.date)

temp_db is being update every two minutes with a new entry thanks to a 
dedicated python script.

I'm trying to develop a website page which would constantly show the last 
value recorded in the table temp_db.

I would like to use ajax to retrieve the last entry (and you will 
understand : without refreshing my webpage)

I've been consulting many tutorials to understand ajax and I think I'm 
pretty ok with the way to do that from the client side.

>From a django standpoint; I'm getting trouble in transferring the last 
entry of my table to the client. I think that json could help me but when 
I'm trying to convert my entry into json with the following view code...

def myview1(request):
end_range = datetime.now()
range = timedelta(days=1)
beg_range = end_range - range
chart = temp_db.objects.filter(date__gte=beg_range)
last_entry = chart.latest('date')
data = serializers.serialize('json', last_entry)
return JsonResponse(data, safe=False)

...Django returns the following error :

Internal Server Error: /myview1/
Traceback (most recent call last):
  File 
"/home/pi/.virtualenvs/django/local/lib/python2.7/site-packages/django/core/handlers/exception.py",
 
line 41, in inner
    response = get_response(request)
  File 
"/home/pi/.virtualenvs/django/local/lib/python2.7/site-packages/django/core/handlers/base.py",
 
line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File 
"/home/pi/.virtualenvs/django/local/lib/python2.7/site-packages/django/core/handlers/base.py",
 
line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/pi/Projects/dashboard/views.py", line 55, in myview1
    data = serializers.serialize('json', last_entry)
  File 
"/home/pi/.virtualenvs/django/local/lib/python2.7/site-packages/django/core/serializers/__init__.py",
 
line 129, in serialize
    s.serialize(queryset, **options)
  File 
"/home/pi/.virtualenvs/django/local/lib/python2.7/site-packages/django/core/serializers/base.py",
 
line 80, in serialize
    for count, obj in enumerate(queryset, start=1):
TypeError: 'temp_db' object is not iterable


I guess that the problem is that one single entry is trying to be 
serialized since when I'm trying to serialize temp_db.objects.all(). -> it 
works. Maybe json is not the the way to do this but the only thing I would 
like to do is to properly get my object from a client side and use it like 
Myentry.tempext in the Js code of the client webpage

Thanks in advance for your help.

Stéphane




-- 
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/870930f9-df04-4018-8ea2-38baef73529d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to