当我尝试在模板中打印 usera._meta.get_all_field_names() 中的所有变量时,它给了我比预期多 2 个变量,我不知道为什么。
型号:
class Clovek(models.Model):
user=models.OneToOneField(User)
ulice=models.CharField(max_length=100, blank=True, null=True)
cislo_popisne=models.CharField(max_length=100, blank=True, null=True)
mesto=models.CharField(max_length=100, blank=True, null=True)
psc=models.CharField(max_length=100, blank=True, null=True)
telefonni_cislo=models.CharField(max_length=100, blank=True, null=True)
zdravotni_pojistovna=models.CharField(max_length=100, blank=True, null=True)
模板中的输出:
['cislo_popisne', 'id', 'mesto', 'psc', 'student', 'telefonni_cislo', 'ucitel', 'ulice', 'user', 'zdravotni_pojistovna']
最佳答案
您很可能有另一个模型(或多个模型),其外键为 Clovek
,且具有反向关系名称 ucitel
和 student
。
get_all_field_names()
documentation非常清楚:
Returns a list of all field names that are possible for this model (including reverse relation names)
关于python - usera._meta.get_all_field_names() 中的变量比模型中的变量更多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19166251/