gpt4 book ai didi

python - 根据外键函数过滤 Django GenericRelation

转载 作者:太空宇宙 更新时间:2023-11-03 17:55:43 25 4
gpt4 key购买 nike

我想找到与我的角色相关且适合各种类别的属性。最终我想要这个输出:

"Attributes": {
"Physical": {
"Level": 1,
"Strength": 1,
"Dexterity": 1,
"Stamina": 1
},
"Mental": {
"Level": 2,
"Intelligence": 1,
"Wits": 1,
"Resolve": 1
},
"Social": {
"Level": 3,
"Presence": 1,
"Manipulation": 1,
"Composure": 1
}
},

我有一个 Class/Enum( AutoNumber ),其中包含属性,以及查找哪个是哪个的方法:

class AttributeAbility(models.Model):

class Attributes(AutoNumber):
INTELLIGENCE = () # Mental, Power
WITS = () # Mental', 'Finesse
RESOLVE = () # Mental', 'Resistance
STRENGTH = () # Physical', 'Power
DEXTERITY = () # Physical', 'Finesse
STAMINA = () # Physical', 'Resistance
PRESENCE = () # Social', 'Power
MANIPULATION = () # Social', 'Finesse
COMPOSURE = () # Social', 'Resistance

attribute = EnumField(Attributes)

@property
def attribute_type(self):
attribute_group = lambda attribute: (
int((attribute.value - 1) / 8)) + 1 % 3

return Category(attribute_group(self.attribute))

class Category(AutoNumber):
MENTAL = ()
PHYSICAL = ()
SOCIAL = ()

我使用这些类将 AttributeAbility 与我的角色连接起来:

class CrossCharacterMixin(models.Model):
cross_character_types = models.Q(app_label='mage', model='mage')
content_type = models.ForeignKey(ContentType, limit_choices_to=cross_character_types,
null=True, blank=True)
object_id = models.PositiveIntegerField(null=True)
content_object = GenericForeignKey('content_type', 'object_id')

class Meta:
abstract = True

class CharacterAttributeLink(Trait, CrossCharacterMixin):
MIN = 1
PRIORITY_CHOICES = (
(0, 'Unassigned'), (1, 'Primary'), (2, 'Secondary'), (3, 'Tertiary')
)
attribute = models.ForeignKey('AttributeAbility')
priority = models.PositiveSmallIntegerField(
choices=PRIORITY_CHOICES, default=0
)

def __str__(self):
return self.attribute.attribute.label

然后在法师上我有:

attributes = GenericRelation('CharacterAttributeLink')

@property
def physical_attributes(self):
type_id = Category['PHYSICAL']
return self.attributes.filter(attribute_type=type_id)

但我收到错误:无法将关键字“attribute_type”解析为字段。选项包括:attribute、attribute_id、content_type、content_type_id、current_value、id、maximum_value、object_id、priority

我的函数如下:

@property
def physical_attributes(self):
type_id = Category['PHYSICAL']
return self.attributes.filter(attribute__attribute_type=type_id)

我收到此错误:相关字段查找无效:​​attribute_type 这有一定道理(尽管我在文档中看到了这一点:>>> Entry.objects.filter(blog_id =4))。

在末尾添加__exact,给我这个:关系字段不支持嵌套查找....此时我迷失了。我需要 custom manager ?我需要将我的 physical_attribute 函数移到其他地方吗?

最佳答案

我最终创建了一个自定义管理器:

class CategoryManager(models.Manager):

'''
Class to manage instances that rely on the category enum
'''

def physical(self):
return [categorised_item for categorised_item in super(CategoryManager, self).get_queryset().all()
if categorised_item.category == Category['PHYSICAL']]

def mental(self):
return [categorised_item for categorised_item in super(CategoryManager, self).get_queryset().all()
if categorised_item.category == Category['MENTAL']]

def social(self):
return [categorised_item for categorised_item in super(CategoryManager, self).get_queryset().all()
if categorised_item.category == Category['SOCIAL']]

然后将其添加到我的 AttributeAbility 模型中:

objects = CategoryManager()

并在我的角色模型上定义此属性:

@property
def social_skills(self):
return [self.skills.filter(skill=skill) for skill
in SkillAbility.objects.social()]

关于python - 根据外键函数过滤 Django GenericRelation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28476966/

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