gpt4 book ai didi

python - Django 在服务器上返回 FieldDoesNotExist 错误但在 shell 中传递正常

转载 作者:行者123 更新时间:2023-11-28 18:49:32 25 4
gpt4 key购买 nike

我正在尝试获取具有一组特定 FK 字段的 ValuesQuerySet,这些 FK 字段跨越多个表之间的关系。当我在 django shell 中运行此查询时,甚至没有打嗝。但是,当在开发服务器上运行时,Django 会在一些(但不是全部)这些 FK 字段上返回 FieldDoesNotExist 错误。

以下是相关模型(我使用的是标准 Django 用户模型):

class Profile(models.Model):

user = models.OneToOneField(User)
first_name = models.CharField(max_length=150,null=True)
middle_name = models.CharField(max_length=150,null=True)
last_name = models.CharField(max_length=150,null=True)
headline = models.TextField(null=True)
connections = models.ManyToManyField('self',through="Connection",symmetrical=False,related_name="connections+")
status = models.CharField(max_length=15,default="active")

class Position(models.Model):

entity = models.ForeignKey(Entity,related_name="positions")
person = models.ForeignKey(User,related_name="positions")
careers = models.ManyToManyField("Career",related_name="positions")
title = models.CharField(max_length=150,null=True)
summary = models.CharField(max_length=450,null=True)
description = models.TextField(null=True)
current = models.BooleanField(default=True)
start_date = models.DateField(null=True)
end_date = models.DateField(null=True)
type = models.CharField(max_length=450,null=True)
degree = models.CharField(max_length=450,null=True)
field = models.CharField(max_length=450,null=True)
status = models.CharField(max_length=15,default="active")
created = models.DateTimeField(auto_now_add=True, null=True)
updated = models.DateTimeField(auto_now=True, null=True)

class Entity(models.Model):

name = models.CharField(max_length=450)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=15,default="active")

class Picture(models.Model):

person = models.ForeignKey(Profile,related_name="pictures")
pic = models.ImageField(max_length=450,upload_to=_get_picture_path)
source = models.CharField(max_length=45,null=True)
description = models.TextField(null=True)
license = models.TextField(null=True)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=15,default="active")

在一个 View 中,我运行两个略有不同的查询来获取一部分用户以及从这些连接表中提取的信息(一个仅限于一组用户 ID,user_ids):

some_people = User.objects.values('id','profile__headline','profile__first_name','profile__last_name','profile__pictures__pic','positions__entity__id','positions__entity__name').filter(id__in=user_ids).annotate(no_of_pos=Count('positions__id')).order_by('-no_of_pos')

all_people = User.objects.select_related('positions','profile').values('id','profile__headline','profile__first_name','profile__last_name','profile__pictures__pic','positions__entity__id','positions__entity__name').annotate(no_of_pos=Count('positions__id')).order_by('-no_of_pos')

这些功能几乎相同,一个仅限于一组预定义的用户(由 id 标识),另一个则不是。我知道这些结果会返回某些信息的重复项,但我会在稍后的步骤中处理这一点,当我将这些信息组装到略有不同的模板字典中时

如果我在 shell 中运行这些查询,则没有问题。查询不会花费任何时间,并且所有字段都会得到正确评估。但是,开发服务器返回此错误:

User has no field named 'headline'

只要包含“profile_headline”、“profile_pictures_pic”、“positions_entity_name”,就会返回类似的错误。这个错误对我来说没有任何意义。这些字段确实存在,但不在用户对象上——而且它们永远不会在用户对象上调用。另外,为什么只有这些字段? “profile_last_name”、“profile__first_name”和“positions_entity_id”在我看来应该都属于同一个容器。

请帮我解决这个问题——有什么想法吗?

我在 Mac OS X 上运行 Django 1.4.2 和 Python 2.6。

最佳答案

发生的情况是您在开发服务器上拥有的模型定义与您拥有的物理数据库模式不匹配。可能是因为您添加了新字段但尚未迁移它们。

要检查,请运行 ./manage.py migrate --list。如果是这种情况,开发服务器上的迁移应该可以解决问题。

关于python - Django 在服务器上返回 FieldDoesNotExist 错误但在 shell 中传递正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15034509/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com