On Fri, Jan 27, 2012 at 9:26 AM, MikeKJ <mike.jo...@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
<clell...@gmail.com>

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

Reply via email to