I am working on creating a Django forums project and I have a "settings" app which access the database for a table called settings.
The table is setup with a verbose_name, name, and value columns. The first is simply a name to be displayed to admins, the second is the name that the setting is accessed by (and is the primary key on the table.)
Now, I have some settings which are boolean, some which are integers, and some of which are string. However, value is a TEXT type in the Database so Django is returning it as a string. Right now I have this implementation to convert booleans into the Python boolean type:
class SettingsMiddleware:
def process_request(self, request):
request.settings = {}
settings = Setting.objects.all()
for setting in settings:
if setting.value == ("True" or "1"):
setting.value = True
if setting.value == ("False" or "0"):
setting.value = False
request.settings[setting.name] = setting.value
return None
So I have two questions:
- Is there a better way to go about this?
- How can I determine whether the string contains an integer? I know that I can convert it to an integer with
int()if it is valid, but how can I tell whether or not it is valid?
Obviously, regarding question number two, I would remove the or "1" and or "0" bits of the current evaluation.