views:

40

answers:

2

I'm trying to change a specific field from a field in an object that I retrieved from a django db call.

class Dbobject ()
   def __init__(self):
       dbobject = Modelname.objects.all()
   def test (self):
       self.dbobject[0].fieldname = 'some new value'

then I am able to access a specific attribute like so:

objclass = Dbobject()
fieldvalue = dbobject.dbobject[0].fieldname

but I want to be able to use the "test" method of the Dbobject class to try to change the specific value on an object's attribute value, but it isn't changing it. I am stumped by this as this is how I thought I am supposed to change an object's attribute value.

Any advice is appreciated

A: 

I am not totally sure what you are trying to achieve, but shouldn't it be something like:

class Dbobject ():
   def __init__(self):
       self.dbobject = Modelname.objects.all()
   def test (self):
       self.dbobject[0].fieldname = 'some new value'
lazerscience
my mistake, I did have it with self in my method in the class, I just copied it over wrong as I had been trying it from an instance of the class also (outside the class, where the instance was 'dbobject'), either way, it doesn't work which is the issue and I can't figure out why
Rick
did you also put the 'self' in the __init__ method?
lazerscience
A: 

I'm not sure if this is the problem or not, but I think you might be missing a save() method.

from models import Person
p = Person.objects.get(pk=100)
p.name = 'Rico'
p.save()      # <== This writes it to the db. Is this what you're missing?

Above is the simple case. Adapted for what you wrote above, it'd be like:

dbobject.dbobject[0].fieldname = 'some new value'
dbobject.dbobject[0].save()

or, I'd write it more like:

rec = dbobject.dbobject[0]
rec.fieldname = 'some new value'
rec.save()

Also note that depending on whether and how you are using transactions, you may or may not see a change to the database until you commit.

Unoti
I'm not trying to update the DB with this, just to change the value within the object, I wrote another method to take the object and save that to the db, the idea is that I want to just update the object with multiple different field values and then to eventually save all at once (by using the other method I wrote)
Rick
I guess what I'm trying to do is sort of like emulating the equivalent of a get.all call which doesn't seem to exist in Django, I guess I just don't understand why I can't change values of the object that is created from the objects.all (just on my local side, as I know I can't use this to update the actual DB)
Rick
You can change the instance value of an object, no problem. Something is going wrong with your extra fancy stuff you're doing. Perhaps you're missing a "self." in your init method? Maybe you intended for line 3 to be self.dbobject = Modelname.objects.all(). if that's not it, then I'm unsure of what you're doing here. There definitely is a get.all() call, it's like Person.objects.all(). Can you help me understand what you're asking, if this isn't it?
Unoti