62👍
I think you’re looking for this:
from django.db.models.loading import get_model
model = get_model('app_name', 'model_name')
There are other methods, of course, but this is the way I’d handle it if you don’t know what models file you need to import into your namespace. (Note there’s really no way to safely get a model without first knowing what app it belongs to. Look at the source code to loading.py if you want to test your luck at iterating over all the apps’ models.)
Update for Django 1.7+: According to Django’s deprecation timeline, django.db.models.loading
has been deprecated in Django 1.7 and will be removed in Django 1.9. As pointed out in Alasdair’s answer,
In Django 1.7+, there is an applications registry. You can use the apps.get_model
method to dynamically get a model:
from django.apps import apps
MyModel = apps.get_model('app_label', 'MyModel')
51👍
For Django 1.7+, there is an applications registry. You can use the apps.get_model
method to dynamically get a model.
from django.apps import apps
MyModel = apps.get_model('app_label', 'MyModel')
- [Django]-Change Django ModelChoiceField to show users' full names rather than usernames
- [Django]-Django, ImportError: cannot import name Celery, possible circular import?
- [Django]-Radio buttons in django Forms
- [Django]-Restrict django FloatField to 2 decimal places
- [Django]-Are sessions needed for python-social-auth
- [Django]-Can I use Socket.IO with Django?
1👍
Classes are “first class” objects in Python, meaning they can be passed around and manipulated just like all other objects.
Models are classes — you can tell from the fact that you create new models using class statements:
class Person(models.Model):
last_name = models.CharField(max_length=64)
class AnthropomorphicBear(models.Model):
last_name = models.CharField(max_length=64)
Both the Person
and AnthropomorphicBear
identifiers are bound to Django classes, so you can pass them around. This can useful if you want to create helper functions that work at the model level (and share a common interface):
def print_obj_by_last_name(model, last_name):
model_name = model.__name__
matches = model.objects.filter(last_name=last_name).all()
print('{0}: {1!r}'.format(model_name, matches))
So print_obj_by_last_name
will work with either the Person
or AnthropomorphicBear
models. Just pass the model in like so:
print_obj_by_last_name(model=Person, last_name='Dole')
print_obj_by_last_name(model=AnthropomorphicBear, last_name='Fozzy')
- [Django]-Best way to write an image to a Django HttpResponse()
- [Django]-How to put comments in Django templates?
- [Django]-Cannot access django app through ip address while accessing it through localhost
0👍
model = django.authx.models.User
? Django returns an error, “global
name django is not defined.”
Django does not return the error. Python does.
First, you MUST import the model. You must import it with
from django.authx.models import User
Second, if you get an error that django
is not defined, then Django is not installed correctly. You must have Django on your PYTHONPATH
or installed in your Python lib/site-packages.
To install Django correctly, see http://docs.djangoproject.com/en/dev/intro/install/#intro-install
- [Django]-UUID('…') is not JSON serializable
- [Django]-How to properly use the "choices" field option in Django
- [Django]-Django py.test does not find settings module
-3👍
If you have the model name passed as a string I guess one way could be
modelname = "User"
model = globals()[modelname]
But mucking about with globals() might be a bit dangerous in some contexts. So handle with care 🙂
- [Django]-Django – after login, redirect user to his custom page –> mysite.com/username
- [Django]-How to update multiple fields of a django model instance?
- [Django]-Extend base.html problem