Skip to content
Discussion options

You must be logged in to vote

You can't just keep re-fetching the object from the database - each will be a fresh object and won't have any changes applied. For example, Device.objects.get(id=66).save() will fetch a fresh copy of the object from the database, and then immediately save it (without any changes). The objects you had previously loaded, but not saved into any variable, will be garbage-collected.

Instead: load the object and assign it to a variable, mutate that object, and then save it. Untested:

d = Device.objects.get(id=66)
d.snapshot()
d.config_template = ConfigTemplate.objects.get(id=2)
d.full_clean()
d.save()

(The "snapshot" is required if you want change logging to work)

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@llamafilm
Comment options

Answer selected by llamafilm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants