Caching/Reload problem - cannot refresh model data

720 views
Skip to first unread message

puppriss

unread,
Sep 13, 2011, 2:00:27 AM9/13/11
to django...@googlegroups.com
Greetings All,

Now, you may think me a bit of an asshat for doing it this way, but....

I have a MySQL db backend, DJANGO admin, and a self-coded desktop application ('InsectApp') written in Python. The 2 apps, DJANGO-Admin & InsectApp, both use the same 'settings' file and access the same data (I coded InsectApp so it would use the DJANGO database API because I thought it would make life easier!).

The problem: is that there are now 2 ways of entering data, one via either app. For example, I'm in InsectApp and have imported model 'species' (InsectApp.models.speices) - meanwhile I add a new 'species' record via the Admin. On returning to InsectApp, I cannot access the new record unless I exit and restart the app. I have tried reloading InsectApp.models but it doesn't work.

I'm assuming it's something to do with caching the data on first import of model 'species', but I have no CACHES in my settings.py. In any case both apps use the same settings.py.

Can anyone explain and/or help me? Is there even a way of doing it?
Thanks in advance

p.s. I need the Admin site because we need certain database info, eg species taxonomy, to be updated remotely by taxonomists. The InsectApp is for rapid entering of large amounts of data wihtin our local network, which would be too clunky using the Admin alone. Hence the 2 apps

Ilian Iliev

unread,
Sep 14, 2011, 3:12:57 AM9/14/11
to django...@googlegroups.com
How are you pulling the data from the database in the desktop app - using pure SQL or using the ORM?

If using the ORM it is possible it to cache the results of the database so you can run:

mymodel.objects.update()

This should clear the cache.

--
eng. Ilian Iliev
Web Software Developer

Mobile: +359 88 66 08 400
Website: http://ilian.i-n-i.org


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/eSnLEH5uZBkJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.




puppriss

unread,
Sep 14, 2011, 4:12:16 AM9/14/11
to django...@googlegroups.com
Brilliant - that does the trick! Many thanks Ilian. A very simple solution yet I trawled the documentation and never came across it. Must've been looking in the wrong place. Yes, I'm using the ORM... and occasionally raw SQL for the app.

Cheers
Phil

Tom Evans

unread,
Sep 14, 2011, 7:34:06 AM9/14/11
to django...@googlegroups.com
On Wed, Sep 14, 2011 at 8:12 AM, Ilian Iliev <il...@i-n-i.org> wrote:
> How are you pulling the data from the database in the desktop app - using
> pure SQL or using the ORM?
>
> If using the ORM it is possible it to cache the results of the database so
> you can run:
>
> mymodel.objects.update()
>
> This should clear the cache.
>

Clear 'the cache' - as the side effect of performing a zero row update
on your model!

I wouldn't do that. The issue the user is hitting is that he is still
within his original transaction - there is no caching going on. Read
the documentation on managing transactions:

https://docs.djangoproject.com/en/1.3/topics/db/transactions/

In particular, how to completely disable transaction management, which
you would probably want to do on the desktop app, and manage them
manually if you need transactions.

https://docs.djangoproject.com/en/1.3/topics/db/transactions/#how-to-globally-deactivate-transaction-management

Cheers

Tom

Ilian Iliev

unread,
Sep 14, 2011, 1:57:04 PM9/14/11
to django...@googlegroups.com
Hi Tom,

I checked the link you send and probably you are right but
can you explain why this is a bad solution?
Yes using autocommit() or disabling transactions seems more right
are there any downsides of the update() that you are seeing?
Image a case when you want to update the result set without breaking the
transaction(is this possible)?

I am not trolling just asking?

Regards,
Ilian Iliev

-- 
eng. Ilian Iliev
Web Software Developer

Mobile: +359 88 66 08 400
Website: http://ilian.i-n-i.org

--
You received this message because you are subscribed to the Google Groups "Django users" group.

Tom Evans

unread,
Sep 15, 2011, 4:43:51 AM9/15/11
to django...@googlegroups.com
On Wed, Sep 14, 2011 at 6:57 PM, Ilian Iliev <il...@i-n-i.org> wrote:
> Hi Tom,
> I checked the link you send and probably you are right but
> can you explain why this is a bad solution?
> Yes using autocommit() or disabling transactions seems more right
> are there any downsides of the update() that you are seeing?
> Image a case when you want to update the result set without breaking the
> transaction(is this possible)?
> I am not trolling just asking?
> Regards,
> Ilian Iliev

It's bad for future maintenance. Other developers may not know that
trick, and be confused why you are doing a 'pointless' update, without
grokking the unannounced side effect. It is always better to be
explicit rather than implicit.

Any UPDATE or INSERT will always commit the current transaction.

Cheers

Tom

Ilian Iliev

unread,
Sep 15, 2011, 4:56:01 AM9/15/11
to django...@googlegroups.com
"Any UPDATE or INSERT will always commit the current transaction." ???

If this is correct(which I doubt) then you do not have transactions at all. As I know
the idea of transaction is to run multiple statements in a way that they are independent form
the other statements run at the same time?


--
eng. Ilian Iliev
Web Software Developer

Mobile: +359 88 66 08 400
Website: http://ilian.i-n-i.org


Cheers

Tom

Tom Evans

unread,
Sep 15, 2011, 5:25:03 AM9/15/11
to django...@googlegroups.com
On Thu, Sep 15, 2011 at 9:56 AM, Ilian Iliev <il...@i-n-i.org> wrote:
> "Any UPDATE or INSERT will always commit the current transaction." ???
>
> If this is correct(which I doubt) then you do not have transactions at all.
> As I know
> the idea of transaction is to run multiple statements in a way that they are
> independent form
> the other statements run at the same time?
>

You're right, I hadn't had my coffee :/

Cheers

Tom

Reply all
Reply to author
Forward
0 new messages