Django class models.textChoices adding extra fields __new__ () positional argument error

I am having trouble with passing parameters to a Django models.textChoices class type; I get errors in my ___next__() override when I try to add extra fields:

TypeError: MyPets.FleaPlans.__new__() missing 1 required positional argument: 'label'

The paramaters should match the tuple data being passed. The default is two params value, label but I want a 3rd, another value, desc, label. No matter what order I do I get a positional parameter error.

If I pass just 2 params in the __new__(cls, value, label) function it does not error but if I pass three params __new__(cls, value, desc, label) it errors. It seems to be whatever is the last parameter, if I change the order.

I can set it to a string so it seems the error is on passing params, reading my tuple list.

My debugging data is just 2 entries for testing and I commented out different __new__ and __init__ test code.

Link to Search example for adding extra fields

Suggested method:

from django.db import models

class ColorChoices(models.TextChoices):
    def __new__(cls, value, label, hex_code):
        obj = str.__new__(cls, value)
        obj._value_ = value
        obj.label = label
        obj.hex_code = hex_code
        return obj

    RED = 'R', 'Red', '#FF0000'
    GREEN = 'G', 'Green', '#00FF00'
    BLUE = 'B', 'Blue', '#0000FF'

# Usage
print(ColorChoices.RED.hex_code)  # Output: #FF0000

Code:

from django.db import models
# from accounts import CustomUser
from django.apps import apps
from django.conf import settings
from django.core.validators import MinValueValidator, MaxValueValidator
from datetime import datetime, timedelta , date
from dateutil.relativedelta import relativedelta
from django.utils import timezone

class MyPets(models.Model):

    class SexChoices(models.TextChoices):
        FEMALE = 'F', 'Female'
        MALE = 'M', 'Male'
        UNSPECIFIED = 'U', 'Unspecified'
        # You can add more comprehensive options as needed

     class FleaPlans(models.TextChoices):
        REVOLUTION = ('REV','Revolution - Monthly treatment/ every 31 days','Revolution')
        NONE = ('NON','No Treatment plan selected','None')

    def __new__(cls,value,desc,label):
                obj = str.__new__(cls,value)
                obj._value_ = value
                obj._label_ = label
    
                obj.desc = "description"
                # obj.cycle = "cycle"
                return obj
    

# it's used in this field
#
    flea_treatment_plan = models.CharField( max_length=3, default=FleaPlans.NONE,
choices=FleaPlans.choices, verbose_name='flea treatment plan')

Easiest way to see the error is via Python/Django Shell:

class FleaPlans(models.TextChoices):
    ...<44 lines>...
            return obj
  File "/home/edwardjs55/.virtualenvs/pet_venv/lib/python3.13/site-packages/django/db/models/enums.py", line 49, in __new__
    cls = super().__new__(metacls, classname, bases, classdict, **kwds)
  File "/usr/local/lib/python3.13/enum.py", line 568, in __new__
    enum_class = super().__new__(metacls, cls, bases, classdict, **kwds)
  File "/usr/local/lib/python3.13/enum.py", line 268, in __set_name__
    enum_member = enum_class._new_member_(enum_class, *args)
TypeError: MyPets.FleaPlans.__new__() missing 1 required positional argument: 'label'
Вернуться на верх