TextChoice Field Not Updating in Django Admin

I have a custom user model that includes an account_type field like this:

class User(AbstractUser):
    # ...
    account_type = models.CharField(max_length=2, choices=AccountType.choices, default=AccountType.BASIC, null=True)
    # ...

I have an AccountType defined as follows outside the User model:

class AccountType(models.TextChoices):
    BASIC = 'BA', 'basic'
    STANDARD = 'ST', 'standard'
    SUPPORTER = 'SU', 'supporter'
    PROFESSIONAL = 'PR', 'professional'

    def __str__(self):
        return str(self.name)

I can modify this field on a user from the shell using a valid value, i.e. ‘PR’ or AccountType.PROFESSIONAL and save the user.

The value is saved to the database, however when I refresh the django admin page, it still shows basic.

If I use the django admin page to choose an item from the account_type field and save it using the Save and Continue Editing Button, the page does correctly set the widget value following the form post. If I refresh, the value is also shown correctly.

If I then check this in my shell, yes, the value was indeed saved to the DB.

If I then manually set the value and user.save() in the shell, and reload the page, it remains showing the value last submitted.

I’ve tested both the default admin.ModelAdmin and a version using fieldsets including this field.

I’ve sanity checked this by toggling a boolean field tos_agreement on the user model and it updates just fine on refresh in admin.

Stumped on this, any ideas?

No specific ideas, but perhaps a couple things to try that might help identify where any why this is happening.

First, a couple of questions:

  • What database are you using?
  • Which versions of Python and Django?
  • When using the shell, are you using the “standard” shell, or shell_plus (from Django extensions)?
  • How are you running your server? (runserver/runserver_plus? gunicorn? uwsgi? something else?)

My gut reaction to this is there’s some type of cache that isn’t being flushed / refreshed properly.

Now, if I were trying to diagnose this, some of the things I’d try:

  • Get the object to a second variable and see if the change is reflected there.
  • After making the change, restart your runserver instance.
  • After making the change in the shell, quit the shell
  • Open up a second console window running a completely separate instance before making the change, make the change in window 1 then get the object in window 2.
  • Open up a third console window (or close your shell in the second) and start the shell after the change has been made, see if a get retrieves the changed value.

None of these are going to provide a solution, just (hopefully) will help identify where the problem is occurring.)

Hey Ken,

I haven’t yet had a chance to return and run through them yet, but will.

I did want to acknowledge that I have seen this and thank you for the quick response and helpful troubleshooting ideas.

This is the second time relatively recently you’ve come back with some useful feedback on something I was working on. You rock.