﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
34099	update_or_create() not saving data assigned in a model's save() method	Phil Gyford	Sarah Boyce	"In the current main branch (testing from commit 37c5b8c) it seems that a model's field assigned in its custom `save()` method isn't being saved to the database.

I have this model, which should set `nickname` to the value of `name` on save:

{{{#!python
from django.db import models

class Person(models.Model):

    name = models.CharField(blank=True, max_length=255)

    nickname = models.CharField(blank=True, max_length=255)

    def save(self, *args, **kwargs):
        self.nickname = self.name
        super().save(*args, **kwargs)
}}}

And here's a test which should pass, and does pass with Django 4.1:

{{{#!python
from django.test import TestCase
from .models import Person

class MyTestCase(TestCase):
    def test_name(self):
        person = Person.objects.create(name=""Bob"")

        self.assertEqual(person.nickname, ""Bob"")

        updated_person, created = Person.objects.update_or_create(
            id=person.id, defaults={""name"": ""Terry""}
        )
        updated_person.refresh_from_db()

        self.assertEqual(updated_person.name, ""Terry"")
        self.assertEqual(updated_person.nickname, ""Terry"")  # Fails here
}}}

But it fails:

{{{
➜  ./manage.py test
Found 1 test(s).
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
F
======================================================================
FAIL: test_name (myapp.tests.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File ""/Users/phil/Projects/personal/django-tester/myproject/myapp/tests.py"", line 16, in test_name
    self.assertEqual(updated_person.nickname, ""Terry"")
AssertionError: 'Bob' != 'Terry'
- Bob
+ Terry
}}}

If the line `updated_person.refresh_from_db()` is removed in the test, the test passes.

So it seems that `update_or_create()` is returning the correctly updated object, but for some reason it's not being saved to the database.

I'm using SQLite for this test, although I first noticed the problem in a project using Postgresql."	Bug	closed	Database layer (models, ORM)	dev	Release blocker	fixed		Florian Apolloner Sarah Boyce Simon Charette David Wobrock	Ready for checkin	1	0	0	0	0	0
