我想更改此查询以返回 RELATED 字段:
msgs = my_unit.messages_in_the_queue.all().select_related("message_content__description")
然而,
>>> msgs.values()[0]
{... 'message_type_id': 31, 'object_id': 4, ...} # <--where is the "description"??
和
>>> msgs.values("message_content__description")[0]
FieldError: Cannot resolve keyword 'message_content__description' into field. Choices are: acknowledged_time, id, message_type, object_id, sent_time, destination
到目前为止,我发现的唯一解决方案是手动使用它:
for queued_message in MyUnit.messages_in_the_queue.all().select_related():
do_something_with(queued_message.message_content.description)
但是,这不是 QuerySet,因此不可分页 -有没有办法将其变成返回所需相关字段的可分页查询?
以下是模型:
class MessageQueueModel(models.Model):
destination = models.ForeignKey(MyUnit, related_name="messages_in_the_queue")
sent_time = models.DateTimeField(default=None, null=True, blank=True)
acknowledged_time = models.DateTimeField(default=None, null=True, blank=True)
#the message:
message_type = models.ForeignKey(ContentType, null=True, blank=True)
object_id = models.PositiveIntegerField(null=True, blank=True)
message_content = generic.GenericForeignKey('message_type', 'object_id')
class CommandModel(models.Model):
description = models.CharField(max_length=100)
command_xml = models.TextField()
任何帮助将非常感激!