0

I'm trying to update an instance of a model in django , however if I try to do this directly I get the Manager not accessible through an instance (as I believe the model manager is a class method).

However what is the best way to update an instance of a model and return the updated instance for use later?

eg

address = Address.objects.create(building_name=building_name, postcode=postcode)
address_updated = Address.objects.update(pk=address.id, **defaults)
print(address_updated.street)

i.e. the address_updated variable seems a bit superfluous and pk=address.id doesn't feel quite right.

Yunti
  • 6,761
  • 12
  • 61
  • 106

2 Answers2

1

If you just need to update a few fields in that model, this may be the best approach:

address = Address.objects.create(building_name=building_name, postcode=postcode)

address.street = "new street"
address.save()
pygeek
  • 7,356
  • 1
  • 20
  • 41
0

If u are looking to update particular variable of a class -

address.Column_Name= Value
address.save()

You can return same address object with updated value.