【问题标题】:Django object filter not recognizing related nameDjango 对象过滤器无法识别相关名称
【发布时间】:2017-03-31 21:25:42
【问题描述】:

使用 Django 1.7

此控制台日志说明了该问题。无法识别“职位”相关字段。

    x = Person.objects.get(PersonSk=452)

    x.Positions
    <django.db.models.fields.related.RelatedManager object at 0x0000000005B4A358>

    x.Associations
    <django.db.models.fields.related.RelatedManager object at 0x0000000005AEDC18>

    Person.objects.filter(Associations__isnull=True)
    [lots of peeps]

    Person.objects.filter(Positions__isnull=True)
raise_field_error
    "Choices are: %s" % (name, ", ".join(available)))
FieldError: Cannot resolve keyword 'Positions' into field.

这里是 Person、PersonAssociation 和 Position 的模型类。为什么它适用于 PersonAssociation 而不是 Position?

class Person(models.Model):
    class Meta:
        managed = False
        db_table = 'PERSON].[Person'
        ordering = ('LastName', 'FirstName')

    PersonSk = models.AutoField(primary_key=True, editable=False)
    FirstName = CharNullField('First Name', max_length=30, blank=True)
    LastName = CharNullField('Last Name', max_length=30, blank=True)
    Email = CharNullField(max_length=50, blank=True)
    PhoneWork = CharNullField('Work Phone', max_length=20, blank=True)
    PhoneMobile = CharNullField('Mobile Phone', max_length=20, blank=True)


class PersonAssociation(models.Model):
    class Meta:
        managed = False
        db_table ='CLIENT].[PersonAssociation'

    PersonAssociationSk = models.AutoField(primary_key=True, editable=False)
    Client = models.ForeignKey(Client, db_column='ClientCode', related_name='Associations')
    Person = models.ForeignKey(Person, db_column='PersonSk', related_name='Associations')


class Position(models.Model):
    class Meta:
        managed = False
        db_table = 'PERSON].[Position'

    PositionSk = models.AutoField(primary_key=True, editable=False)
    Person = models.ForeignKey(Person, db_column='PersonSk', related_name='Positions')
    ConsumerClient = models.ForeignKey(Client, db_column='ClientCode', related_name='Positions')
    ProviderClient = models.ForeignKey(Client, db_column='ProviderClientCode', related_name='ProviderPositions', blank=True, null=True)
    Role = models.ForeignKey(ContactRole, db_column='ContactRoleSk', related_name='Positions')
    Facility = models.ForeignKey(Facility, db_column='FacilitySk', blank=True, null=True)
    Notes = models.TextField(blank=True, null=True)
    StartDate = models.DateField()
    EndDate = models.DateField(blank=True, null=True)

据我所知,related_name 字段的设置方式是否相同?我还尝试将 Position 中的 related_name 更改为“Plops”,然后重新进行测试,结果相同。

这是另一个我认为更能说明问题的控制台日志。在 Positions 模型中,我将 Role 字段和 Person 字段配置为具有相同的相关名称。所以我尝试了这个:

ContactRole.Positions
<django.db.models.fields.related.ForeignRelatedObjectsDescriptor object at 0x00000000060F5DA0>

Person.Positions
<django.db.models.fields.related.ForeignRelatedObjectsDescriptor object at 0x00000000060F5CC0>

ContactRole.objects.filter(Positions__isnull=True)
[ lots of roles ]

Person.objects.filter(Positions__isnull=True)
FieldError: Cannot resolve keyword 'Positions' into field.

所以看起来它的过滤器功能不起作用?

【问题讨论】:

  • 请注意,您的各个字段名称不应大写或驼峰式。相反,对空格使用小写值和下划线。

标签: django django-models


【解决方案1】:

您的问题是Positions 不是一个字段,而是一个关系。如果您想要一个职位为零的员工列表,则需要进行聚合,然后对该聚合进行过滤。

类似这样的:

Person.objects.annotate(
    positions_count=Count('Positions')
).filter(positions_count=0)

【讨论】:

    猜你喜欢
    • 2010-09-20
    • 2019-02-14
    • 2018-11-24
    • 2023-02-06
    • 2018-11-18
    • 2022-06-12
    • 1970-01-01
    相关资源
    最近更新 更多