----
Steps to reproduce:
- In a new app, define the following model:
{{{
class CoinTossResult(models.Model):
class CoinFace(models.Choices):
HEADS = "h"
TAILS = "t"
result = models.CharField(
max_length=1,
choices=CoinFace.choices,
default=CoinFace.HEADS,
)
}}}
- Run makemigrations multiple times. After the initial migration, an extra
migration is generated repeatedly:
{{{
(myapp) ~/work/myapp % ./manage.py makemigrations
Migrations for 'foo':
foo/migrations/0001_initial.py
- Create model CoinTossResult
(myapp) ~/work/myapp % ./manage.py makemigrations
Migrations for 'foo':
foo/migrations/0002_alter_cointossresult_result.py
- Alter field result on cointossresult
(myapp) ~/work/myapp % ./manage.py makemigrations
Migrations for 'foo':
foo/migrations/0003_alter_cointossresult_result.py
- Alter field result on cointossresult
(myapp) ~/work/myapp % ./manage.py makemigrations
Migrations for 'foo':
foo/migrations/0004_alter_cointossresult_result.py
- Alter field result on cointossresult
(myapp) ~/work/myapp % cat
foo/migrations/0002_alter_cointossresult_result.py
# Generated by Django 4.2.2 on 2023-07-13 11:00
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('foo', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='cointossresult',
name='result',
field=models.CharField(choices=[('h', 'Heads'), ('t',
'Tails')], default='h', max_length=1),
),
]
(myapp) ~/work/myapp % diff
foo/migrations/0002_alter_cointossresult_result.py
foo/migrations/0003_alter_cointossresult_result.py
9c9
< ('foo', '0001_initial'),
---
> ('foo', '0002_alter_cointossresult_result'),
(myapp) ~/work/myapp % diff
foo/migrations/0003_alter_cointossresult_result.py
foo/migrations/0004_alter_cointossresult_result.py
9c9
< ('foo', '0002_alter_cointossresult_result'),
---
> ('foo', '0003_alter_cointossresult_result'),
}}}
----
Same steps while using the workaround:
{{{
(myapp) ~/work/myapp % git diff
diff --git a/foo/models.py b/foo/models.py
index 50e0ad0..e553ad7 100644
--- a/foo/models.py
+++ b/foo/models.py
@@ -8,5 +8,5 @@ class CoinTossResult(models.Model):
result = models.CharField(
max_length=1,
choices=CoinFace.choices,
- default=CoinFace.HEADS,
+ default=CoinFace.HEADS.value,
)
(myapp) ~/work/myapp % ./manage.py makemigrations foo
Migrations for 'foo':
foo/migrations/0001_initial.py
- Create model CoinTossResult
(myapp) ~/work/myapp % ./manage.py makemigrations foo
No changes detected in app 'foo'
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/34710>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Comment (by David Sanders):
Hi Div Shekhar,
In order to get your example working you'd need to specify the type like
so:
{{{
class CoinTossResult(models.Model):
class CoinFace(str, models.Choices):
...
}}}
I don't think this is an issue with Django – though there may be some
documentation updates that could make this clearer when defining custom
`Choices` classes. I'll leave it up to felixx or nessita to decide whether
this ticket is invalid 😊
--
Ticket URL: <https://code.djangoproject.com/ticket/34710#comment:1>
Comment (by Div Shekhar):
My bad - you can close this out.
I was thrown coming from django-model-utils's Choices and incorrectly
assumed TextChoices had extra features I didn't need.
--
Ticket URL: <https://code.djangoproject.com/ticket/34710#comment:2>
* status: new => closed
* resolution: => invalid
Comment:
Closing following latest comment from reporter.
--
Ticket URL: <https://code.djangoproject.com/ticket/34710#comment:3>