27👍
✅
from user.models import User
user = User.objects.create_user(tel='1234567890', password='YKH0000000')
print(user._meta.model)
<class 'user.models.User'>
print(user._meta.model.__name__)
User
print(user.__class__.__name__)
User
👤Ykh
1👍
To stay close to the question, type(instance).__name__
is a valide answer (which is the one given in this older question)
So using @Ykh example:
from user.models import User
user = User.objects.create_user(tel='1234567890', password='YKH0000000')
print(type(user).__name__)
User
0👍
With __name__
, you can get a model name in str
type as shown below:
from .models import MyModel
print(MyModel.__name__) # MyModel
- Django – null value in column violates not-null constraint in Django Admin
- Difference in sequence of query generated in Django and Postgres for select_for_update
- Django Method Not Allowed (POST)
- Django-allauth retrieve avatar profile pictures
- Django – How to get the max_length of a field
Source:stackexchange.com