mymodel.save() does not work django view but works in python shell

5,682 views
Skip to first unread message

NoviceSortOf

unread,
May 11, 2009, 11:05:00 AM5/11/09
to Django users

.save() is not working for me either with models or with forms in
django.

I'll start with a simple example, on a single non related table.

from views.py

def testit():
from myproject.models import NewCust
p1 = NewCust() ## Create blank instance
p1.zipcode = '2222222' ## Load something into it.
p1.last_name = 'Smith'
p1.save() ## Save it

In Django this does not work.

Now from the Python command line
it works like clockwork.

>>> from myproject.models import NewCust
>>> p1 = NewCust() ## Create blank instance
>>> p1.zipcode = '2222222' ## Load something into it.
>>> p1.last_name = 'Smith'
>>> p1.save() ## Save it
...creates a record every time...

My attempts to get this to work have included deleting
NewCust from my PostGres SQL tables and revalidating
and recreating them via manage.py to make sure their
is not problem in the table.

I'm baffled why this simple data operation as outlined in
Django Chapter 5 - Interacting with a Database does not
work in this case.

Any clues appreciated.

NoviceSortOf

unread,
May 11, 2009, 11:32:23 AM5/11/09
to Django users

An observation I've made it that django does update the related
sequence_id table of the table but does not add a row to the table.

Karen Tracey

unread,
May 11, 2009, 11:48:43 AM5/11/09
to django...@googlegroups.com
On Mon, May 11, 2009 at 11:32 AM, NoviceSortOf <dljons...@gmail.com> wrote:


An observation I've made it that django does update the related
sequence_id table of the table but does not add a row to the table.

How are you deciding it's not working?  Are you seeing errors?  Does the object have a pk assigned after you call save()?  If yes, have you tried adding code after the save() to retrieve the object by pk? Are you perhaps querying for the save() working using a transaction that is isolated (perhaps using a repeatable-read level of isolation) from the update transaction and therefore doesn't see the other transaction's updates? 

This is very basic fundamental function, it is unlikely to be badly broken.  It's far more likely that what you are doing to check that it is working is what is broken.

Karen

Michael

unread,
May 11, 2009, 1:01:57 PM5/11/09
to django...@googlegroups.com
On Mon, May 11, 2009 at 11:05 AM, NoviceSortOf <dljons...@gmail.com> wrote:

from views.py

def testit():
   from myproject.models import NewCust
   p1 = NewCust()              ## Create blank instance
   p1.zipcode = '2222222' ## Load something into it.
   p1.last_name = 'Smith'
   p1.save()                          ## Save it

If this is actually the view in your views.py, I can guarantee that it is not being called. If it was it would be throwing errors. A views statement always expects one argument, request. So I would first look at my urls and make sure that the url matches what you are trying to get and then make sure that it passes to testit. Then I would look at your traceback and start figuring out where things are.

Basic debugging is finding a solution that works and slowly make it more complicated until you find where your error is. 

I hope that helps,

Michael

NoviceSortOf

unread,
May 11, 2009, 1:22:28 PM5/11/09
to Django users

Thanks for your reply, I'm thinking and working outloud on
the answers that follow, I've a solution sort of but remain mystified
as to why and how it works.

> How are you deciding it's not working?

via pgAdminIII where I can see the sequence table
being incremented + 1 on every django save but
no rows added to table mytable

>Are you seeing errors?

No errors on the surface it looks like it working but
no rows are being added.

I can refresh and reboot and still see no rows being
added to Table mytable, even though Sequences id table
gets incremented.

I've also added an
assert False, p1.id
to the routine in question, and can see it is adding an id to the
sequence table as corroborated in the
p1.id resulting from p1.save()...

>>>Does the object have a pk assigned after you call save()?  

Yes it has a pk assigned after the save reflecting it is infact
incrementing ids.

>>>If yes, have you tried > adding code after the save() to retrieve the object by pk?

p2 = mytable.objects.get(id=p1.id)

Returns the object as if it is saved by I see nothing in the table,
python command line though issued from the same folder,
using the same settings does though add a row.

>Are you perhaps querying for the save() working using a >transaction that is isolated?

I'm not sure if I completely understand this question this
though is an -- isolated function defined as "testit" in views
pointed to by url.py. There is no code before or after this
sample, it is not nested.

I'm sorry I don't understand exactly what you are saying in this
question...this code though has been stripped down to as basic
as possible to though to determine why django does not
save when python command line saves to the table.

> This is very basic fundamental function, it is unlikely to be badly broken. It's far more likely that what you are doing to check that it is working is what is broken.

I hear what you are saying, pgAdminIII though has been accurate for
all other functions regarding seeing what is in the database via
django and python over the last 5 months, including ; additions to
registration table and profile entries, adding tables via manage.py
and about ever other function I can imagine.

I'm baffled why my own attempts to add data to the tables is not
working and/or why is the data saved via save() in django is
disappearing when save() in python shell save() works every time.

Now get this -- if I add a 'return' to the end of the function it adds
the rows -- why is this?

If I insert an assert False the functions sort of behave like they are
working returning an object for the new pk id but incrementing the seq
tables, but not loading the data into postgresql.

any idea why this is?


NoviceSortOf

unread,
May 11, 2009, 1:32:15 PM5/11/09
to Django users
> Michael

I appreciate your answer...

I omited in my code example was the assert False that was being used
to debug the problem, so intuitively you were right something was
wrong with the code

With the assert False though it was bringing up the debugger and I
could view the variables. Without the return statement in the function
data was not being commited to a save visable in Postgres Sql.

> from views.py

> def testit():
> from myproject.models import NewCust
> p1 = NewCust() ## Create blank instance
> p1.zipcode = '2222222' ## Load something
> p1.last_name = 'Smith'
> p1.save() ## Save it
> # assert False, p1.id
return ## NOTE: save does not commit
## without return on Postgre Sql

If anyone can explain to me why this is, it could help, I've been
using assert False to attempt to debug something that apparently does
not work
if assert False is present.

Karen Tracey

unread,
May 11, 2009, 3:02:52 PM5/11/09
to django...@googlegroups.com
On Mon, May 11, 2009 at 1:22 PM, NoviceSortOf <dljons...@gmail.com> wrote:
Now get this -- if I add a 'return' to the end of the function it adds
the rows -- why is this?

This, combined with the sequence number increasing but being unable to actually see rows added, makes me think perhaps without the return wherever you have added it the code is going down some path that is causing the transaction you are in to be rolled back instead of being committed.
 

If I insert an assert False the functions sort of behave like they are
working returning an object for the new pk id but incrementing the seq
tables, but not loading the data into postgresql.

any idea why this is?

Does your Django view use the commit_on_success decorator?  Or are you doing manual transaction management and somehow not committing the transaction?  That would cause the transaction to be rolled back in the case of any error (which an assert False would trigger).  I'd have to check (which I don't have time for at the moment) but I think Postgres is one of the DBs where rollback doesn't affect sequence numbers that have been incremented during a transaction, which would explain why you see that incrementing without seeing the actual objects being saved.

Karen
Reply all
Reply to author
Forward
0 new messages