saving in a view

2 views
Skip to first unread message

MikeKJ

unread,
Jan 27, 2012, 12:26:19 PM1/27/12
to django...@googlegroups.com
I have 2 models Count is purely an incremental counter a model called Sale,
the reason for the Count models is to increment a reference for Sale and
other models

In a view I get the latest sale and count then I want to save the incemented
count (number) to Count also save number to Sale

customer =
Sale.objects.all().order_by('-id').filter(email_address=email).distinct()0
this = Count.objects.all().order_by('-id')0
number = this.number + 1
a = this.save(force_insert=True)
ref = number
reference = number
name = customer.name
salutation = customer.salutation
b = customer.save(force_update=True)

I know the field names are rubbish but can anyone spot where I am going
wrong please

--
View this message in context: http://python.6.n6.nabble.com/saving-in-a-view-tp4343606p4343606.html
Sent from the django-users mailing list archive at Nabble.com.

Ian Clelland

unread,
Jan 27, 2012, 1:29:12 PM1/27/12
to django...@googlegroups.com
On Fri, Jan 27, 2012 at 9:26 AM, MikeKJ <mike....@paston.co.uk> wrote:
I have 2 models Count is purely an incremental counter a model called Sale,
the reason for the Count models is to increment a reference for Sale and
other models

In a view I get the latest sale and count then I want to save the incemented
count (number) to Count also save number to Sale

 
First off, the instances of "0" at the end of the first two lines should be [0] (surrounded by square brackets), but I'm assuming that that's an artifact of the email transport.

Past that, your code doesn't actually *do* anything, other than pull two objects out of the database, and save them again, unmodified. Let's look at the method you've pasted, line by line:

customer = Sale.objects.all().order_by('-id').filter(email_address=email).distinct()[0]
this = Count.objects.all().order_by('-id')[0]

This part retrieves two objects from the database. You may want to guard these two lines with an exception handler -- if there are no matching Sale objects, or if the Count table is empty, either of these lines could raise a KeyError.

number = this.number + 1

This line creates a new local variable called number, and assigns it the value of this.number + 1. It doesn't do anything to the 'this' object at all.

a = this.save(force_insert=True)

So this line forces 'this' to be re-saved (remember, though, that it hasn't been modified, so the save doesn't really do anything). Model.save() doesn't return anything, so 'a' is assigned the value None.

ref = number
reference = number
name = customer.name
salutation = customer.salutation

Again, these just create some new local variables, but they don't actually change your customer object at all, so this line:

b = customer.save(force_update=True)

Just forces a re-save of the unmodified customer object.

I"m not sure exactly what it is that you're trying to do with this view, but I think that you are going to need some lines that actually update the 'this' and 'customer' object. You would do that like this:

    this.number = this.number + 1

or
    customer.reference = number

Then, when you save the objects (and just use "customer.save()", the force_update=True isn't really necessary), then the new values you have assigned will be saved in the database.

-- 
Regards,
Ian Clelland
<clel...@gmail.com>
Reply all
Reply to author
Forward
0 new messages