Django runserver needs restart on psql data changes

45 views
Skip to first unread message

Arnab Banerji

unread,
Apr 29, 2015, 2:39:05 PM4/29/15
to django...@googlegroups.com
Hi all - I am working on a Django app that interacts with a psql database via a set of models. 

(1) There is a backend job which keeps updating the database via the same models. 

(2) I am creating a table with a view using django_tables2 - and giving it a custom dictionary to render using data from the psql database (via my Django models). 

(3) I am using 'runserver' fpr testing out my code. 

The rendering happens perfectly, but the problem is - when the client user does a browser refresh, the new data does not get reloaded until I restart the runserver. Is there something I am doing wrong with respect to getting the data from the database and into my view?

Appreciate any help you might give me on this topic,
-AB

Javier Guerra Giraldez

unread,
Apr 29, 2015, 4:09:26 PM4/29/15
to django...@googlegroups.com
On Wed, Apr 29, 2015 at 12:35 PM, Arnab Banerji <arn...@gmail.com> wrote:
> The rendering happens perfectly, but the problem is - when the client user
> does a browser refresh, the new data does not get reloaded until I restart
> the runserver. Is there something I am doing wrong with respect to getting
> the data from the database and into my view?


sounds like you're storing that data in a variable. you should either
read it from database on every request, or get some cache invalidation
techniques.

--
Javier

Arnab Banerji

unread,
Apr 29, 2015, 5:13:40 PM4/29/15
to django...@googlegroups.com
Hi Javier - thanks for the response. Yes - I am storing the data in a dictionary - then having django_tables2 API render the dictionary as a table. How do I force a read of that variable for every browser refresh on the client side? 

Thanks for your time,
-AB

Carl Meyer

unread,
Apr 29, 2015, 6:10:21 PM4/29/15
to django...@googlegroups.com
Hi Arnab,

On 04/29/2015 03:13 PM, Arnab Banerji wrote:
> Hi Javier - thanks for the response. Yes - I am storing the data in a
> dictionary - then having django_tables2 API render the dictionary as a
> table. How do I force a read of that variable for every browser refresh
> on the client side?

Using a variable is fine; the problem is that (I'm guessing) you are
creating that variable at the top level of the module. Such code is only
run once, when the module is first imported.

You need to make sure that all database queries are run only inside view
functions (or methods of view classes), never at the top-level of a module.

carl

signature.asc

Arnab Banerji

unread,
Apr 29, 2015, 11:34:31 PM4/29/15
to django...@googlegroups.com
Hi Carl - that was *exactly* what I was doing - thanks a ton for finding that issue even without requiring the code. Now that I think about it, putting that definition in the global section makes absolutely no sense.

Appreciate your feedback,
-AB
Reply all
Reply to author
Forward
0 new messages