Desciption:
I have a generic function
def gen(model_name,model_type):
objects = model_name.objects.all()
for object in objects:
object.model_type = Null (Or some activity)
object.save()
How Can I achieve the above ? Is it possible?
I would use get_model
:
from django.db.models import get_model
mymodel = get_model('some_app', 'SomeModel')
As of Django 1.7 the django.db.models.loading
is deprecated (to be removed in 1.9) in favor of the the new application loading system. The 1.7 docs give us the following instead:
$ python manage.py shell
Python 2.7.6 (default, Mar 5 2014, 10:59:47)
>>> from django.apps import apps
>>> User = apps.get_model(app_label='auth', model_name='User')
>>> print User
<class 'django.contrib.auth.models.User'>
>>>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With