0
from app.models import Person
person = Person.objects.get(name='Carl')
person.name = 'John'
person.save()

Now, there is a new, empty Person entry in the database with the name John, but none of the other attributes that 'Carl' has (age, address, etc.). Furthermore, the original Carl object isn't changed at all.

How can I just change what the person's name attribute is?

AmagicalFishy
  • 1,249
  • 1
  • 12
  • 36
  • 1
    What you describe is not the behaviour of this code. – Daniel Roseman Jul 30 '18 at 15:30
  • 1
    This doesnt seem like normal behavior - a new instance should only be created when calling an existing instance's `.save` if you clear out the pk first, e.g. `person.id = None; person.save()` Can you share the code for your person model? – Joseph Jul 30 '18 at 15:30
  • @Joseph In the code above, I *am* calling an existing instance's `save` method (the existing instance is the Person who's name attribute is Carl). – AmagicalFishy Jul 30 '18 at 15:38
  • But doing that does not give the result you claim. You at least need to show your models. – Daniel Roseman Jul 30 '18 at 15:54
  • @amagicalfishy there was a qualifier on my statement: "if you clear out the pk first". You are not clearing out the pk in the code you showed, therefore a new instance should not be created and instead the existing instance will be updated. This is Django's normal behavior. If you are getting different results, there must be something else going on. – Joseph Jul 30 '18 at 16:07

0 Answers0