python - usera._meta.get_all_field_names() 中的变量比模型中的变量更多

标签 python django django-models

当我尝试在模板中打印 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,且具有反向关系名称 ucitelstudent

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/

相关文章:

python - 如何在 Django 中按父类别订购模型?

python - 如何使用特定用户拥有的对象创建 ModelForm

python - AWS EC2连续运行Python程序一个月

python - 名称实体识别NLTK回顾

python - 创建 super 用户时出现 Django 错误,AttributeError : 'Manager' object has no attribute 'get_by_natural_key'

django - 在 Django 管理 ListView 中避免 n+1 查询

django - 如何将 Model.viewset 中的查找字段更改为 Django Rest Framework 中的其他唯一参数?

python - Python尝试除错误外,程序崩溃

python - 对尝试循环一个空的可迭代对象采取行动的惯用方式

django - 代码 : unknown, 错误:从 "api.twitter.com"获取请求 token 时响应无效 - 如何获取和打印 Twitter 错误消息