下面是一个数据结构,可以通过使用 Django Rest Framework 构建的端点进行访问:
template <typename T>
void func(int start_size, int idx)
{
cout << start_size << " " << idx << endl;
}
template <typename T, typename T2, typename... Args> // Addition of T2, and change sizeof...
void func(int start_size, int idx)
{
if ((idx + 1) == int(start_size - int(1 + sizeof...(Args)))) {
cout << start_size << " " << idx << endl;
return;
}
func<T2, Args...>(start_size, idx);
}
我的模型如下:
配置
"sites": [{
"id": 1,
"configs": [
{
"id": 1,
"subconfigs": [
{
"id": 1,
"name": "subconfig_1",
"macro_subconfigs": [1, 2]
"flag": true
},
{
"id": 2,
"name": "subconfig_2",
"macro_subconfigs": [1]
"flag": false
},
{
"id": 3,
"name": "subconfig_3",
"macro_subconfigs": [2]
"flag": false
},
],
"macro_subconfigs": [
{
"id": 1,
"flag": true,
"subconfigs": [
{
"id": 1,
"name": "subconfig_1",
"macro_subconfigs": [1, 2]
"flag": true
},
{
"id": 2,
"name": "subconfig_2",
"macro_subconfigs": [1]
"flag": false
},
],
"name": "macro_subconfig_1"
},
{
"id": 2,
"flag": false,
"subconfigs": [
{
"id": 1,
"name": "subconfig_1",
"macro_subconfigs": [1, 2]
"flag": true
},
{
"id": 3,
"name": "subconfig_3",
"macro_subconfigs": [2]
"flag": false
},
],
"name": "macro_subconfig_2"
},
]
}
]
}]
SubConfig
class Config(TimeStampedModel):
site = models.ForeignKey("site.Site", related_name="configs")
@property
def macro_subconfigs(self):
try:
return MacroSubConfig.objects.filter(subconfigs__config__pk=self.pk).distinct()
except MacroSubConfig.DoesNotExist:
return tuple()
MacroSubConfig
class SubConfig(TimeStampedModel):
name = models.CharField(max_length=200)
config = models.ForeignKey(
"Config", related_name="subconfigs", on_delete=models.CASCADE
)
flag = models.BooleanField(default=True)
还有我的序列化器:
class MacroSubConfig(TimeStampedModel):
name = models.CharField(max_length=100)
subconfigs = models.ManyToManyField(SubConfig, related_name="macro_subconfigs")
flag = models.BooleanField(default=True)
在我的class SiteConfigSerializer(serializers.HyperlinkedModelSerializer):
configs = ConfigSerializer(many=True)
class Meta:
model = Site
fields = ("name", "configs")
class SubConfigSerializer(serializers.ModelSerializer):
class Meta:
model = SubConfig
fields = ("name")
class ConfigSerializer(serializers.ModelSerializer):
subconfigs = SubConfigSerializer(many=True)
macro_subconfigs = MacroSubConfigSerializer(many=True)
class Meta:
model = Config
fields = ("name", "subconfigs", "macro_subconfigs")
class MacroSubConfigSerializer(serializers.HyperlinkedModelSerializer):
subconfigs = serializers.PrimaryKeyRelatedField(
many=True, queryset=SubConfig.objects.all()
)
class Meta:
model = MacroSubConfig
fields = ["subconfigs", "name"]
中,我想在具有SiteViewSet
的macro_subconfigs列表中过滤macro_subconfigs
。我已经设法对子配置列表中的flag: true
做同样的事情。
预期结果如下:
subconfigs
如何从"sites": [{
"id": 1,
"configs": [
{
"id": 1,
"subconfigs": [
{
"id": 1,
"name": "subconfig_1",
"macro_subconfigs": [1, 2]
"flag": true
}
],
"macro_subconfigs": [
{
"id": 1,
"flag": true,
"subconfigs": [
{
"id": 1,
"name": "subconfig_1",
"macro_subconfigs": [1, 2]
"flag": true
},
{
"id": 2,
"name": "subconfig_2",
"macro_subconfigs": [1]
"flag": false
},
],
"name": "macro_subconfig_1"
}
]
}
]
}]
的Config对象的列表中过滤元素?
如有需要,随时问我更多的精度。谢谢
更多信息
我在ViewSet中尝试过的内容
SiteViewSet