Accessing Django models using DJANGO_SETTINGS_MODULE

25 views
Skip to first unread message

Dheerendra Rathor

unread,
Mar 8, 2015, 12:41:30 PM3/8/15
to django...@googlegroups.com
I've a django project and I've few models like ChatRoom and Chat etc

I'm accessing the ChatRooms from commandline using ipython as follow
In [1]: import os

In [2]: os.environ['DJANGO_SETTINGS_MODULE'] = 'chat_project.settings'

In [3]: from chat.models import Chat, ChatRoom

In [4]: ChatRoom.objects.all()
Out[4]: [<ChatRoom: Lab 01>, <ChatRoom: Lab 02>, <ChatRoom: Lab 03>, <ChatRoom: Lab 04>, <ChatRoom: Lab 05>, <ChatRoom: Lab 06>, <ChatRoom: Lab 07>, <ChatRoom: Lab 10>, <ChatRoom: Lab 13>, <ChatRoom: Lab 14>, <ChatRoom: Lab 15>, <ChatRoom: Lab 16>]

Now, I'm adding more ChatRooms through web interface - Lab 17, Lab 18 and deleting Lab 10
Now the new output of 
In [5]: ChatRoom.objects.all()

The output is still the same
Out[5]: [<ChatRoom: Lab 01>, <ChatRoom: Lab 02>, <ChatRoom: Lab 03>, <ChatRoom: Lab 04>, <ChatRoom: Lab 05>, <ChatRoom: Lab 06>, <ChatRoom: Lab 07>, <ChatRoom: Lab 10>, <ChatRoom: Lab 13>, <ChatRoom: Lab 14>, <ChatRoom: Lab 15>, <ChatRoom: Lab 16>]

the webinterface is running as wsgi application on gunicorn server. 

I think the output on command line is not changed because the django default model manager is accessing the objects from cache and not from database itself. Is there anyway I can get the updated models here on command line? Or can I force django ORM to fetch objects from database and not from cache?

Collin Anderson

unread,
Mar 10, 2015, 5:09:36 PM3/10/15
to django...@googlegroups.com
Hi,

Hmm... maybe try ChatRoom.objects.all().all() ? :)

If you run this, you can see if it's actually running sql or not:
import logging; logging.basicConfig(level=logging.DEBUG)

Are you sure you're using the same database? :)

Collin

Tom Evans

unread,
Mar 11, 2015, 10:04:04 AM3/11/15
to django...@googlegroups.com
I don't think that is likely. What version of django do you use, what
database engine do you use, what options have you configured it to
use? Django version matters a lot in this case.

It sounds like the shell is running within a transaction, so that
reads are repeatable within it - doing the same query will always
return the same results. This is the purpose of transactions, nothing
changes in the database until you commit your transaction.

Cheers

Tom
Reply all
Reply to author
Forward
0 new messages