Re: Why can't "directly" change array elements of model objects in Django shell?

2011-10-12 Thread fei
Instead, you can use the get() function, x = MyModel.objects.get(id=3) x.some_field = "new value" x.save() The difference between all() and get() is that all() will do a lazy evaluation and get() will hit the physical database. Fei On Oct 12, 11:57 pm, Tom Evans wrote: > On Fri, Oct 7, 2011 at

Re: Why can't "directly" change array elements of model objects in Django shell?

2011-10-12 Thread Tom Evans
On Fri, Oct 7, 2011 at 10:01 PM, Chris Seberino wrote: > > > On Oct 7, 10:39 am, Jacob Kaplan-Moss wrote: >> On Fri, Oct 7, 2011 at 10:30 AM, Chris Seberino wrote: >> >> > I've noticed that this doesn't often work in Django shell... >> >> > x = MyModel.objects.all() >> > x[3].some_field = "new v

Re: Why can't "directly" change array elements of model objects in Django shell?

2011-10-07 Thread Andre Terra
The easiest way would be to slice the list exactly how you were doing with x[3]... It doesn't get much easier than that. I guess if you really need to spare yourself three keystrokes, you can do it like this: >>> x = YourModel.objects.all()[3] >>> x.foo = 'bar' >>> x.save() Cheers, AT On Fri,

Re: Why can't "directly" change array elements of model objects in Django shell?

2011-10-07 Thread Chris Seberino
On Oct 7, 10:39 am, Jacob Kaplan-Moss wrote: > On Fri, Oct 7, 2011 at 10:30 AM, Chris Seberino wrote: > > > I've noticed that this doesn't often work in Django shell... > > > x = MyModel.objects.all() > > x[3].some_field = "new value" > > x[3].save() > This is because `MyModel.objects.all()` i

Re: Why can't "directly" change array elements of model objects in Django shell?

2011-10-07 Thread Javier Guerra Giraldez
On Fri, Oct 7, 2011 at 10:39 AM, Jacob Kaplan-Moss wrote: >> *Sometimes* the first works and I don't know why. > > This is because `MyModel.objects.all()` isn't a list; it's a QuerySet. > That is, `MyModel.objects.all()` *doesn't* hit the database until you > say `x[3]`, at which point Django perf

Re: Why can't "directly" change array elements of model objects in Django shell?

2011-10-07 Thread Jacob Kaplan-Moss
On Fri, Oct 7, 2011 at 10:30 AM, Chris Seberino wrote: > > I've noticed that this doesn't often work in Django shell... > > x = MyModel.objects.all() > x[3].some_field = "new value" > x[3].save() > > Rather, I must often do this... > > temp = x[3] > temp.some_field = "new value" > temp.save() > >

Why can't "directly" change array elements of model objects in Django shell?

2011-10-07 Thread Chris Seberino
I've noticed that this doesn't often work in Django shell... x = MyModel.objects.all() x[3].some_field = "new value" x[3].save() Rather, I must often do this... temp = x[3] temp.some_field = "new value" temp.save() *Sometimes* the first works and I don't know why. Please advise. Chris -- Y