字段正向或反向取值

class UserInfo(models.Model):
    user_type_choices = (
        (1,'讲师'),
        (2,'班主任'),
        (3,'学生'),
    )
    user_type_id = models.IntegerField(choices=user_type_choices)
    user = models.CharField(max_length=32)
    pwd = models.CharField(max_length=64)

class ClassList(models.Model):
    name = models.CharField(max_length=32)
    teacher = models.ForeignKey(to='UserInfo',to_field='id',limit_choices_to={'user_type_id':1},related_name="tclasslist")# user_obj.tclasslist.all()
    headmaster = models.ForeignKey(to='UserInfo',to_field='id',limit_choices_to={'user_type_id':2}) # related_name=None;      user_obj.classlist_set.all()

class Test(models.Model):
    title = models.CharField(max_length=32)
    teacher = models.ForeignKey(to='UserInfo', to_field='id', limit_choices_to={'user_type_id': 3})

class Student(models.Model):
    info = models.OneToOneField(to="UserInfo",to_field="id")
    cls_name=models.ManyToManyField(to="ClassList")

ClassList._meta.get_field("teacher ").model 获取ClassList模型
ClassList._meta.get_field("teacher ") 获取teacher ,不能以ClassList.teacher 的方式获取
->
ClassList._meta.get_field("teacher ").rel.related_name
获取related_name
->
ClassList._meta.get_field("teacher ").rel.to
获取UserInfo
->ClassList._meta.get_field("teacher ").verbose_name获取字段verbose_name

->ClassList._meta.get_field('name') # 根据字段名称,获取字段对象
->ClassList._meta.fields # 获取类中所有的字段
->ClassList._meta._get_fields() # 获取类中所有的字段(包含反向关联的字段)
->Student._meta.many_to_many # 获取正向m2m字段
->Student._meta.many_to_many[0].rel.to #获取ClassList

from django.db.models.fields.reverse_related import ManyToOneRel,ManyToManyRel
    for related_object in UserInfo._meta.related_objects:
        model_name = related_object.field.model._meta.model_name
        related_name = related_object.related_name
        # print(type(related_object))
        print(related_object.field_name)
        print("to------------",related_object.to)
        print("field------------",related_object.field)
        if (type(related_object) == ManyToOneRel):

            field_name = related_object.field_name
            limit_choices_to = related_object.limit_choices_to
            # print(model_name,field_name,related_name,limit_choices_to)
            if model_name == mn and str(related_name) == rn:
                print(field_name,limit_choices_to)
                

UserInfo._meta.related_objects获取所有反向关联字段
related_object.field_name# 获取正向关联字段
related_object.to #获取关联关联model
related_object.field #获取当前下有m2m,fk的model
打印结果

id
to------------ 
field------------ app01.ClassList.teacher
id
to------------ 
field------------ app01.ClassList.headmaster
id
to------------ 
field------------ app01.Test.teacher
id
to------------ 
field------------ app01.Student.info

ModelForm字段也可以反向获取modle

    meta = type('Meta', (object,), {'model': models.ClassList, 'fields': '__all__'})
    TestModelForm = type('TestModelForm', (ModelForm,), {'Meta': meta})
    from django.forms.models import ModelChoiceField
    for item in TestModelForm():
        if isinstance(item.field, ModelChoiceField):
            print(item.field.queryset.model)

打印结果
通过item.field.queryset.model获取对应的字段



你可能感兴趣的:(字段正向或反向取值)