I’m building a tagging system in Django and would like to allow spaces and other characters in the tag name for display but filter them out and use lower case when matching names etc.
To that end I have added a field to my Tag model as so:
class Tag(models.Model):
name = models.CharField(max_length=200, unique=True)
matchname = re.sub("\W+" , "", name.lower())
However I am running into a problem, the CharField is not a string and I cannot for the life of me find out how to convert it to one!
You’re defining a
classthere sonameis not a string it’s a Django Field.Additionally, converting
nametomatchnameat the class level doesn’t make any sense. You should be doing this on the instance.You could add a method to your class to do this: