I have to create a class which instances must fit two conditions:
- being an
str
subclass so that it can be passed toos.listdir()
- being deconstructible so that the string does not appear as-is when django generates migrations, but as
mailing.conf.StrConfRef('another string')
Here is what I tried:
class StrConfRef(str):
def __new__(cls, name, within=None):
value = globals()[name]
if within:
value = within.format(value)
self = str.__new__(cls, value)
self.name = name
self.within = within
return self
def deconstruct(self):
return ('{}.{}'.format(__name__, self.__class__.__name__), (self.name,),
{'within': self.within})
The first point is respected os.listdir(StrConfRef(...))
works. However, it is still evaluated as a "standard string" in migrations. I checked out django.db.migrations.autodetector
and noticed that when the code is executed, it StrConfRef instances reach this line (which is expected, and should mean that StrConfRef is properly deconstructed).
So I wonder why it appears as a string in my migrations, and not a mailing.conf.StrConfRef instance. And how to fulfill my conditions.
PS: If you wonder why I need this behavior, checkout this question.
PS2: I'm runnign Python 3.4 and Django 1.9.2