0

次のリソースが与えられていると仮定します。

class RecipeResource(ModelResource):
    ingredients = fields.ToManyField(IngredientResource, 'ingredients')

    class Meta:
        queryset = Recipe.objects.all()
        resource_name = "recipe"
        fields = ['id', 'title', 'description',]


class IngredientResource(ModelResource):
    recipe = fields.ToOneField(RecipeResource, 'recipe')

    class Meta:
        queryset = Ingredient.objects.all()
        resource_name = "ingredient"
        fields = ['id', 'ingredient',]

myhost.com/api/v1/recipe/?format=jsonへのHTTPリクエストは、次の応答を返します。

{
    "meta":
    {
        ...
    },
    "objects":
    [
       {
           "description": "Some Description",
           "id": "1",
           "ingredients":
           [
               "/api/v1/ingredient/1/"
           ],
           "resource_uri": "/api/v1/recipe/11/",
           "title": "MyRecipe",
       }
   ]
}

ここまでは順調ですね。

しかし、今、私は材料resource_uri( "/ api / v1 / ingredient / 1 /")を次のようなものと交換したいと思います。

{
   "id": "1",
   "ingredient": "Garlic",
   "recipe": "/api/v1/recipe/1/",
   "resource_uri": "/api/v1/ingredient/1/",
}

次の応答を取得するには:

{
    "meta":
    {
        ...
    },
    "objects":
    [
       {
           "description": "Some Description",
           "id": "1",
           "ingredients":
           [
               {
                   "id": "1",
                   "ingredient": "Garlic",
                   "recipe": "/api/v1/recipe/1/",
                   "resource_uri": "/api/v1/ingredient/1/",
               }
           ],
           "resource_uri": "/api/v1/recipe/11/",
           "title": "MyRecipe",
       }
   ]
}
4

1 に答える 1

1

答えは、属性full=Trueです。

材料=fields.ToManyField('mezzanine_recipes.api.IngredientResource'、'材料'、full = True)

于 2012-11-12T16:20:44.630 に答える