我已经创建了一个“角色起源”表,我想对该表运行查询,该查询将返回“三个选择”。每个选择中都有两个随机的Character Origins。
我目前正在使用Django和DRF构建我的API,我尝试使用Django-rest-framework-tricks中的嵌套序列化器,但失败了,现在,我认为最好的选择是使用比Djangos更好的查询ORM可以做到。
Serializers.py
class OriginsNestedSerializer(serializers.ModelSerializer):
class Meta:
model = Origin
fields = (
'id',
'origin',
'ability',
'skill',
'bonus',
'ac',
'fort',
'ref',
'will',
'defense',
'lvl_1',
'lvl_2_or_6',
'novice',
'utility',
'expert',
)
read_only_fields = ['id', ]
nested_proxy_field = True
class ChoiceCharacterCreationSerializer(serializers.ModelSerializer):
origin_primary = OriginsNestedSerializer()
origin_secondary = OriginsNestedSerializer()
class Meta:
model = Origin
fields = (
'origin_primary',
'origin_secondary',
)
nested_proxy_field = True
class ChoicesSerializer(serializers.ModelSerializer):
choice = ChoiceCharacterCreationSerializer()
class Meta:
model = Origin
fields = (
'choice',
)
Views.py
class CreateACharacterViewSet(viewsets.ModelViewSet):
serializer_class = serializers.ChoicesSerializer
queryset = models.Origin.objects.order_by('?')[:3]
我的结果是这个...
[
{
"choice": {
"origin_primary": {
"id": 60,
"origin": "Lagomorph",
"ability": " STR",
"skill": " Perception",
"bonus": " Bio",
"ac": 0,
"fort": 2,
"ref": 2,
"will": 2,
"defense": "You gain a +2 bonus on saving throws.",
"lvl_1": "At the start of each encounter, ",
"lvl_2_or_6": "When you score a critical ",
"novice": "RABBIT PUNCH -",
"utility": "BUNNY HOP - ",
"expert": "VICIOUS STEAK A MILE WIDE - Encounter"
},
"origin_secondary": {
"id": 60,
"origin": "Lagomorph",
"ability": " STR",
"skill": " Perception",
"bonus": " Bio",
"ac": 0,
"fort": 2,
"ref": 2,
"will": 2,
"defense": "You gain a +2 bonus on saving throws.",
"lvl_1": "At the start of each encounter",
"lvl_2_or_6": "When you score a critical ...",
"novice": "RABBIT PUNCH - At-Will • ...",
"utility": "BUNNY HOP - Encounter ... ",
"expert": "VICIOUS STEAK A MILE WIDE - Encounter .."
}
}
},
{
"choice": {
"origin_primary": {
"id": 9,
"origin": "Electrokinetic",
"ability": " WIS",
"skill": " Mechanics",
"bonus": " Dark",
"ac": 0,
"fort": 0,
"ref": 2,
"will": 0,
"defense": "Ref +2",
"lvl_1": "Gain resist 10 electricity.",
"lvl_2_or_6": "When you score a critical hit,...",
"novice": "ELECTRIC BOOGALOO - At-Will ...",
"utility": "STAND CLEAR - Encounter ...",
"expert": "LIGHTNING BOLT - Encounter ..."
},
"origin_secondary": {
"id": 9,
"origin": "Electrokinetic",
"ability": " WIS",
"skill": " Mechanics",
"bonus": " Dark",
"ac": 0,
"fort": 0,
"ref": 2,
"will": 0,
"defense": "Ref +2",
"lvl_1": "Gain resist 10 electricity.",
"lvl_2_or_6": "When you score a critical ....",
"novice": "ELECTRIC BOOGALOO - ...",
"utility": "STAND CLEAR - ...",
"expert": "LIGHTNING BOLT - Encounter ...
}
},
{
"choice": {
"origin_primary": {
"id": 67,
"origin": "Tauric",
"ability": " CON",
"skill": " Athletics",
"bonus": " Bio",
"ac": 0,
"fort": 2,
"ref": 0,
"will": 0,
"defense": "Fort +2",
"lvl_1": "You are treated as ....",
"lvl_2_or_6": "When you score a critical hit,...",
"novice": "KICK - At-Will .....",
"utility": "QUICK DASH - Encounter ....",
"expert": "TRAMPLE - En......"
},
"origin_secondary": {
"id": 67,
"origin": "Tauric",
"ability": " CON",
"skill": " Athletics",
"bonus": " Bio",
"ac": 0,
"fort": 2,
"ref": 0,
"will": 0,
"defense": "Fort +2",
"lvl_1": "You are treated as being m.",
"lvl_2_or_6": "When you score a critical hit, th)",
"novice": "KICK - At-Will ....",
"utility": "QUICK DASH - Encounter ....",
"expert": "TRAMPLE - Encounter ...."
}
}
}
]
我想要的是这样的三个选择,每个选择中有两个彼此不同的随机起源。
我认为这需要完成。
class CreateACharacterViewSet(viewsets.ModelViewSet):
serializer_class = serializers.OriginSerializer
queryset = models.Origin.objects.raw('SELECT * ...")