You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
model_utils.add_status_query_managers used by StatusModel to attach QueryManager instances for each of the names in STATUS assumes that STATUS is either a model_utils.Choices or 2-tuple and ignores the choices_name value set on the status field of the StatusModel, if overridden:
But then we lose the convenience of comparisons between the value of status on a SomeModel instance and either SomeModel.STATUSES by name (e.g. instance.status is SomeModel.STATUSES.ONE) or SomeModel.STATUS, which is an unaddressed 2-tuple.
Ideally, we could use the new enum.Enum based API in Django 3.x AND get the benefits of StatusModel: status_changed timestamps and automatic custom managers for STATUS values just by leveraging the features of StatusField:
fromdjango.dbimportmodelsfrommodel_utilsimportmodelsasextrasclassSomeModel(extras.StatusModel):
STATUS=models.TextChoices("SomeModel.STATUS", ("one", "two", "three"))
status=extras.StatusField(choices_name="STATUS.choices")
# one = model_utils.managers.QueryManager(status=STATUS.ONE) # implicit# two = model_utils.managers.QueryManager(status=STATUS.TWO) # implicit# three = model_utils.managers.QueryManager(status=STATUS.THREE) # implicit
Setting choices_name to STATUS.choices appears to satisfy StatusField -- no error is raised trying to set choices on the field instance -- but add_status_query_managers raises trying to unpack the name and value from STATUS, since it's an enum.Enum and not a 2-tuple or model_extras.models.Choices instance.
As a receiver function add_status_query_managers seems to accept the model class as sender and perform operations on it, so I think we could modify it to at least respect the choices_name of the StatusField. I'm not sure that will address my stated desire to compare status to values of STATUS, TBH, but I'm willing to write a patch to test it out.
The text was updated successfully, but these errors were encountered:
al-the-x
changed the title
StatusModel is ALMOST compatible with Django 3.x TextChoices / IntChoices
StatusModel is ALMOST compatible with Django 3.x TextChoices / IntegerChoices
Nov 27, 2021
model_utils.add_status_query_managers
used byStatusModel
to attachQueryManager
instances for each of the names inSTATUS
assumes thatSTATUS
is either amodel_utils.Choices
or 2-tuple and ignores thechoices_name
value set on thestatus
field of theStatusModel
, if overridden:django-model-utils/model_utils/models.py
Line 95 in 9d0dfc3
StatusModel
could be made compatible withTextChoices
/IntegerChoices
ifadd_status_query_managers
either:TextChoices
/IntegerChoices
instance (e.g. by checking forchoices
on theSTATUS
model)choices_name
on theStatusField
namedstatus
There's a way to hack around it:
But then we lose the convenience of comparisons between the value of
status
on aSomeModel
instance and eitherSomeModel.STATUSES
by name (e.g.instance.status is SomeModel.STATUSES.ONE
) orSomeModel.STATUS
, which is an unaddressed 2-tuple.Ideally, we could use the new
enum.Enum
based API in Django 3.x AND get the benefits ofStatusModel
:status_changed
timestamps and automatic custom managers forSTATUS
values just by leveraging the features ofStatusField
:Setting
choices_name
toSTATUS.choices
appears to satisfyStatusField
-- no error is raised trying to setchoices
on the field instance -- butadd_status_query_managers
raises trying to unpack thename
andvalue
fromSTATUS
, since it's anenum.Enum
and not a 2-tuple ormodel_extras.models.Choices
instance.As a receiver function
add_status_query_managers
seems to accept the model class assender
and perform operations on it, so I think we could modify it to at least respect thechoices_name
of theStatusField
. I'm not sure that will address my stated desire to comparestatus
to values ofSTATUS
, TBH, but I'm willing to write a patch to test it out.The text was updated successfully, but these errors were encountered: