7👍
✅
The simplest translation is:
types = dict('cat' = CatType, 'dog' = DogType)
newobj = types[type](user = user, cat_id = thing_id)
Obviously, this relies on the types taking the same parameters.
3👍
Classes in Python are objects, so you can just have a map mapping names to classes:
animal_type_map = {'cat': VoteCat, 'dog': VoteDog}
Then you can use animal_type_map[type]
just as you would use VoteCat
or VoteDog
.
- [Django]-Upgrading to django 1.11 many to many column not found
- [Django]-Populate CheckboxSelectMultiple with existing data from django model form
- [Django]-How do I move project folders in PyCharm Project View?
- [Django]-"ImportError: No module named cv2" when running Django project from PyCharm IDE
- [Django]-Heroku 'DATABASES' is not defined
Source:stackexchange.com