Skip to content

Switch to django-enumfields2

I had very strange errors when working on !2481 (merged). Normally, using QuerySet.only() is the opposite as a .defer(): it marks every field not included in the list as deferred, so accessing it causes an extra SQL query.

In [1]: Element.objects.only("id").first().name
SELECT "documents_element"."id"
  FROM "documents_element"
 ORDER BY "documents_element"."id" ASC
 LIMIT 1

Execution time: 0.000967s [Database: default]
SELECT "documents_element"."id",
       "documents_element"."name"
  FROM "documents_element"
 WHERE "documents_element"."id" = '00000000-0000-0000-0000-000000000000'::uuid
 LIMIT 21

Execution time: 0.000954s [Database: default]
Out[1]: 'lol'

But when using any EnumField, there is an exception instead:

In [2]: WorkerVersion.objects.only("id").first().feature
SELECT "process_workerversion"."id"
  FROM "process_workerversion"
 ORDER BY "process_workerversion"."id" ASC
 LIMIT 1

Execution time: 0.001040s [Database: default]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[2], line 1
----> 1 WorkerVersion.objects.only("id").first().feature

File ~/.../lib/python3.10/site-packages/enumfields/fields.py:27, in CastOnAssignDescriptor.__get__(self, obj, type)
     25 if obj is None:
     26     return self
---> 27 return obj.__dict__[self.field.name]

KeyError: 'feature'

django-enumfields is not officially unmaintained, but it has not seen any activity for a few years, and does not support Python 3.11+. django-enumfields2 is a fork that supports recent versions of Python and Django, still sees some activity, and fixes this bug.