# TextChoices in Meta class for constraints

**URL:** <https://forum.djangoproject.com/t/textchoices-in-meta-class-for-constraints/46099>\
**Category:** Using the ORM\
**Created:** [September 29, 2026, 3:50pm UTC](https://forum.djangoproject.com/t/textchoices-in-meta-class-for-constraints/46099 "2026-09-29T15:50:15Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![StephanieAG](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/stephanieag/32/17567_2.png) [@StephanieAG](https://forum.djangoproject.com/u/StephanieAG)\
**Post date:** [September 29, 2026, 3:50pm UTC](https://forum.djangoproject.com/t/textchoices-in-meta-class-for-constraints/46099/1 "2026-09-29T15:50:15Z")

</div>

I had a similar problem as [this question on StackOverflow](https://stackoverflow.com/questions/71522816/django-using-a-nested-textchoice-class-values-in-a-meta-class) where I was getting a `NameError` that the `ModelName` wasn’t defined when I tried to migrate the changes after adding a `TextChoices` class and `CheckConstraint` within `Meta` trying to reference the choices (like `ModelName.TextChoiceClassName.CHOICE_A`).

Just wanting to double-check: is the below (found in the StackOverflow response) normally how this problem is handled? So you move the `TextChoices` class outside of the model it’s being used in and then add an `OrderStatusChoices = OrderStatusChoices` so that you can still reference `Order.OrderStatusChoices.CANCELLED` elsewhere?

```python
class OrderStatusChoices(models.TextChoices):
    ...

class Order(models.Model):
    OrderStatusChoices = OrderStatusChoices
    ...

    class Meta:
        constraints = [
            models.CheckConstraint(
                name='%(app_label)s_%(class)s_status_valid',
                check=models.Q(status__in=OrderStatusChoices.values)
            )
        ]

```
