import os if __name__ == '__main__': os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'XadminDemon.settings') import django django.setup() from app01 import models # 获取app的值 ret = models.Book._meta.app_label print(ret) # 获取模型的名称 ret = models.Book._meta.model_name print(ret) # 获取字段的属性 obj = models.Book._meta.get_field("title") ret = obj.verbose_name print(ret) obj = models.Book._meta.get_field("price") ret = obj.verbose_name print(ret) """ app01 book 书名 price """
_meta.get_filed(字段) models的字段
def index(request): book_name = models.Book._meta.get_field("name") # app01.Book.name book_name = book_name.verbose_name # 书名 print("="*20) print(book_name) return HttpResponse("ok")
class Book(models.Model): nid = models.AutoField(primary_key=True) name = models.CharField(max_length=16, verbose_name="书名") price = models.DecimalField(max_digits=4, decimal_places=2) publisher = models.ForeignKey(to="Publish", to_field="nid", on_delete=models.CASCADE) def __str__(self): return self.name
get_filed 由字符串获取字段对象
来源:https://www.cnblogs.com/wt7018/p/11374559.html