у меня имеется кастомный сериалайзер, который работает сразу с двумя моделями, такой запрос фронтера, хочет именно так:
{
capex: {}, // первая форма
object_settings:{}, //вторая форма
}
class CapexSerializer(serializers.Serializer):
capex = ProjectCapexSerializer(read_only=True)
object_settings = CapexObjectSettingsSerializer(read_only=True)
class Meta:
fields = '__all__'
Но я не могу правильно написать функцию list в ListCreateAPIView
class ProjectCapexListView(generics.ListCreateAPIView):
serializer_class = serializers.CapexSerializer
filter_backends = [DjangoFilterBackend]
filterset_fields = '__all__'
permission_classes = [permissions.IsOwnerCapex, IsAuthenticated]
def list(self, request, *args, **kwargs):
user = request.user
capexs = models.ProjectCapex.objects.filter(project__author=user)
object_settings = models.CapexObjectSetting.objects.filter(capex__project__author=user)
capex_serializer = self.get_serializer(capexs, object_settings, many=True)
capex_serializer.is_valid()
return Response(capex_serializer.data)
Но он выдает только capex :(
[
{
"capex": {
"id": 80,
"variant_name": "Новый вариант",
"name": "Новый объект",
"amount_capital_expenditure": 0,
"VAT_rate": 0,
"start_date": null,
"end_date": null,
"VAT_refund": false,
"leasing_switch": false,
"VAT": 0,
"deprication_period": 0,
"liquidation_cost_switch": false,
"liquidation_profit_switch": false,
"liquidation_cost": 0,
"liquidation_cost_VAT_rate": 0,
"liquidation_profit": 0,
"liquidation_profit_VAT_rate": 0,
"taxdeprication_switch": false,
"taxdeprication_period": 0,
"amortization_premium": 0,
"project": 1
}
},
{
"capex": {
"id": 81,
"variant_name": "Новый вариант",
"name": "Новый объект",
"amount_capital_expenditure": 0,
"VAT_rate": 0,
"start_date": null,
"end_date": null,
"VAT_refund": false,
"leasing_switch": false,
"VAT": 0,
"deprication_period": 0,
"liquidation_cost_switch": false,
"liquidation_profit_switch": false,
"liquidation_cost": 0,
"liquidation_cost_VAT_rate": 0,
"liquidation_profit": 0,
"liquidation_profit_VAT_rate": 0,
"taxdeprication_switch": false,
"taxdeprication_period": 0,
"amortization_premium": 0,
"project": 1
}
},
]
А должен выдавать такой ответ:
"results": [
{
"capex": {
"id": 0,
"variant_name": "string",
"name": "string",
"amount_capital_expenditure": 0,
"VAT_rate": 100,
"start_date": "2022-07-13T16:27:20.926Z",
"end_date": "2022-07-13T16:27:20.926Z",
"VAT_refund": true,
"leasing_switch": true,
"VAT": 0,
"deprication_period": 0,
"liquidation_cost_switch": true,
"liquidation_profit_switch": true,
"liquidation_cost": 0,
"liquidation_cost_VAT_rate": 100,
"liquidation_profit": 0,
"liquidation_profit_VAT_rate": 100,
"taxdeprication_switch": true,
"taxdeprication_period": 0,
"amortization_premium": 0,
"project": 0
},
"object_settings": {
"id": 0,
"files": [
{
"id": 0,
"file_size": "string",
"file_name": "string",
"file_format": "string",
"file": "string",
"create_date": "2022-07-13T16:27:20.926Z",
"_object": 0
}
],
"location": "string",
"cadastral_number": "string",
"square": 0,
"construction": "string",
"gcontractor": "string",
"create_date": "2022-07-13T16:27:20.926Z",
"capex": 0
}
}
]
}
подскажите, как это должно быть сделано?