ModelStore Caching/Not-Updating issue....

12 views
Skip to first unread message

Adam

unread,
Aug 3, 2010, 1:46:57 PM8/3/10
to dojango-users
Recently I decided to learn Django and Dojo and in my experimentation,
I have come across a bit of a perplexing issue and was wondering if
anyone would be willing to provide me with some pointers.

So, I have a page that does the following

1.) pull data from a ModelStore into an ItemFileReadStore (works)
2.) dojo pulls a page name and URL from the JSON data (works)
3.) dojo then builds a list of links in html on the page (works well)
4.) dojo builds an ajax form on the page that posts back to the view
(works)
5.) the form call back posts a message and calls a rebuild of the list
(rebuild doesn't go as planned but call back works)

On this page, the dojo form is created in JS. When submitted, it
creates a new entry in the model I used above. When I use this form,
the post, processing and object.save(), and json call back work
great.

Once this form is submitted I would like to repopulate the name/URL
list on the page.

The problem I am having, is even though I can verify positive database
write, the ModelStore doesn't update its objects, so the JSON page
isn't updated and the list is not updated (its regenerated but with
the same data as last time)

I have a feeling this is because I am executing my saves through an
instance of my Model in my View and performing my reads through an
instance of my Model in my Store (2 separate instances of my Model).
I think calling a Model.object.update() in the store would be the work
around hack.

Any thoughts or advice on how to fix this? (code below. There is a
lot of experimentation in this code and could be much improved)

Thanks in advance

Adam

### View Code##
###GET and POST handeling####
###Post = save data, Get = return JSON###

if int(person_id) < 1:
#if we are posting, we are creating
if request.method == 'POST':
#if all required data has been posted
if ("first_name" in request.POST) and ("last_name" in
request.POST):
try:
sal = ""
if 'sal' in request.POST:
sal = request.POST['sal']
fname = request.POST["first_name"]
lname = request.POST["last_name"]
p = Person(salutation=sal, first_name=fname,
last_name=lname)
p.save()
response = HttpResponse('{success:True}',
mimetype='application/json')
response['Cache-Control']='no-cache'
return response;
except:
raise Exception('Cannot Create Person "%s %s"
something exploded....oops' %
(request.POST.first_name,request.POST.last_name))
else:
return HttpResponse("{'success':False}",
mimetype='application/json')

elif request.method == "GET":
#probably should do some error handling here....
#returns everyone
store = PersonStore()
response = HttpResponse(store.to_json(),
mimetype='application/json')
response['Cache-Control']='no-cache'
return response


#####Store###

class PersonStore(Store):
id = StoreField()
salutation = StoreField()
first_name = StoreField()
last_name = StoreField()
full_name = StoreField( get_value=ObjectMethod('full_name'))
url = StoreField( get_value=ObjectMethod('url'))

class Meta(object):
objects=Person.objects.all()

def get_full_name(self, obj):
return obj.full_name()

klipstein

unread,
Aug 3, 2010, 6:29:38 PM8/3/10
to dojango-users
Hi Adam,

I'm not the author of the ModelStore, but I assume it has something to
do with the ObjectMethod and that this is evaluated once and then gets
cached (proxied).

Do last_name and first_name bring up the actual content?

Regards, Tobias

Ben Wilber

unread,
Aug 3, 2010, 6:50:35 PM8/3/10
to dojang...@googlegroups.com

Hello,

After the successful POST are you doing another GET to pull the new Store data?  ItemFileReadStore is probably just reusing the same data it got before.  Try forcing a re-fetch.

Thanks,
Ben

Sent from my phone


--
You received this message because you are subscribed to the Google Groups "dojango-users" group.
To post to this group, send email to dojang...@googlegroups.com.
To unsubscribe from this group, send email to dojango-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/dojango-users?hl=en.

Adam __

unread,
Aug 3, 2010, 9:37:40 PM8/3/10
to dojang...@googlegroups.com
Ben,

Thanks for your response.  While there may still be some ItemFileReadStore issues I still need to ferret out, If I directly poll the view associated with the raw JSON data, it doesn't have the updated data.  I believe I have two model objects and  my POST updates one, while the GET/ModelStore has a second one.  I would have thought, the ModelStore would have died between GET requests but it seems to be persisting, isn't directly updated in my POST and has no reason to refresh it's self.  If I restart the server, it recreates the ModelStore and it will have the pre-restart data.  If I make more updates, it wont populate until I restart again.  

I sent Tobios a more detailed version of my thoughts.  

Ill keep everyone posted if something works out.  

Thank you,

Adam

Adam __

unread,
Aug 3, 2010, 9:26:20 PM8/3/10
to dojang...@googlegroups.com
Tobias,   

Thanks for your response, I think we are moving in the same direction and I think I have found a path forward.  I realized there was a second set of documentation on Google Sites, and it looks like I may need to use the @servicemethod decorator.  I think it will eliminate the second People (django  model) Object.  

 I believe my issue is my GET (ModelStore) and POST (Model) have 2 separate instances of the People object (django model class).  So when I use the POST it updates its People object/model and the DB.  If I query this People object (used in the POST), it has the updated data.  

Then I'll call the GET (ModelStore) to receive the updated json data from the view and it's QuerySet still has the same data (pre post).  I'm guessing it doesn't have the new data because this object instance wasn't directly updated and there was no reason for it to be forced to refresh.  The perplexing part is I don't understand why this object should persist between the first(pre-post) and second call (after-post) but that is how it is behaving.  I would expect the ModelStore would die after the first request and be recreated for the second (after-get) update.  But that doesn't seem to be the case.  

I apologize if this is a bit repetitive but I'm trying to answer all of your points.  Both first_name and last_name return real data from the People model of the Address book app I am writing to learn django/dojo.  
On a GET, the view returns a ModelStore.as_json() including first_name, last_name and others.  A validated POST creates a new person.  Then if i pull another GET, (after a post) the ModelStore has the old data.  

Thank you for your help,

Adam

Adam __

unread,
Aug 4, 2010, 2:11:56 PM8/4/10
to dojang...@googlegroups.com
Everyone,

I think I have this one figured out.  The key was to call "store.set_option('objects', Person.objects.all())" immediately after store instantiation.  It feels a bit odd to do it this way, but seems to hit the reset button and force the store to re-query the database.  

The @servicemethod definitely made my code prettier but was not the solution.  

Here is the view method that handles both the JSON data and the JSON-RPC service.  

def person_data(request, action):
    store = PersonStore()
    store.set_option('objects', Person.objects.all())
    if int(action) == 0:
        return HttpResponse(store.to_json(), mimetype='application/json')
    elif int(action) == 1:
        return HttpResponse('{}&&\n' + store.service(request), mimetype='application/json')
    else:
        raise Exception('Invalid action')

Thanks for all your help and all the work in Dojango, it definitely makes life easier.  

Adam

On Tue, Aug 3, 2010 at 6:29 PM, klipstein <tobias.k...@googlemail.com> wrote:

klipstein

unread,
Aug 4, 2010, 2:41:13 PM8/4/10
to dojango-users
Hey Adam,

great that you've figured out a solution for your problem. It's always
the best way of learning :)

Ben: Strange, that the objects of the store are persistent even after
complete new instantiation. Also need to do some tests, why that could
happen. Maybe you could also have a look at it.

Regards, Tobias

On Aug 4, 8:11 pm, Adam __ <ofr...@gmail.com> wrote:
> Everyone,
>
> I think I have this one figured out.  The key was to call
> "store.set_option('objects', Person.objects.all())" immediately after store
> instantiation.  It feels a bit odd to do it this way, but seems to hit the
> reset button and force the store to re-query the database.
>
> The @servicemethod definitely made my code prettier but was not the
> solution.
>
> Here is the view method that handles both the JSON data and the JSON-RPC
> service.
>
> def person_data(request, action):
>     store = PersonStore()
>     store.set_option('objects', Person.objects.all())
>     if int(action) == 0:
>         return HttpResponse(store.to_json(), mimetype='application/json')
>     elif int(action) == 1:
>         return HttpResponse('{}&&\n' + store.service(request),
> mimetype='application/json')
>     else:
>         raise Exception('Invalid action')
>
> Thanks for all your help and all the work in Dojango, it definitely makes
> life easier.
>
> Adam
>
> On Tue, Aug 3, 2010 at 6:29 PM, klipstein
> <tobias.klipst...@googlemail.com>wrote:
> > dojango-user...@googlegroups.com<dojango-users%2Bunsu...@googlegroups.com>
> > .

Ben Wilber

unread,
Aug 6, 2010, 9:12:15 PM8/6/10
to dojang...@googlegroups.com
Yes it appears that Django is caching the objects in the inner Meta class at compile time.  This affects subsequent queries if the original query object stays resident in memory between requests.  A quick fix would be just to define a property that generates the objects list at runtime.  But this should be handled better at a lower level in the API.  I'll work on this.

Ben

To unsubscribe from this group, send email to dojango-user...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages