1

これは私のモデルです

class Nisit(models.Model):

この

class Page(models.Model):
followingNisit = models.ManyToManyField(Nisit,blank=True)

これは私のリソースです

class NisitResource(ModelResource):
    page = fields.ToManyField('chula.api.PageResource','page_set',null=True)
class Meta:
    queryset = Nisit.objects.all()      
    resource_name = 'nisit'
    filtering = {
        'page' : ALL_WITH_RELATIONS,
        'id' : ALL,
    }

class PageResource(ModelResource):
    followingNisit = fields.ToManyField(NisitResource, 'followingNisit',null=True)
    reporter = fields.ManyToManyField(ReporterResource,'reporter')
    followers_count = fields.CharField(attribute='followers_count')

class Meta:
    queryset = Page.objects.all()
    resource_name = 'page'
    authorization= Authorization()
    filtering = {
        'id':ALL,
        'followingNisit': ALL_WITH_RELATIONS,
    }

-------127.0.0.1:8000/api/v2/page/?format=json&followingNisit__id=1 とリクエストしたらOK

しかし逆に、---------127.0.0.1:8000/api/v2/nisit/?format=json&page__id=1 をリクエストすると、このエラーが発生します

{"error_message": "Cannot resolve keyword 'page_set' into field. Choices are: displayName, facebook, faculty, friend, id, major, n_id, name, page, password, picture, reporter, year_in_school", "traceback": "Traceback (most recent call last):\n\n  File \"D:\\Study\\SeniorProject\\Code\\mysite\\tastypie\\resources.py\", line 202, in wrapper\n    response = callback(request, *args, **kwargs)\n\n  File \"D:\\Study\\SeniorProject\\Code\\mysite\\tastypie\\resources.py\", line 441, in dispatch_list\n    return self.dispatch('list', request, **kwargs)\n\n  File \"D:\\Study\\SeniorProject\\Code\\mysite\\tastypie\\resources.py\", line 474, in dispatch\n    response = method(request, **kwargs)\n\n  File \"D:\\Study\\SeniorProject\\Code\\mysite\\tastypie\\resources.py\", line 1127, in get_list\n    objects = self.obj_get_list(request=request, **self.remove_api_resource_names(kwargs))\n\n  File \"D:\\Study\\SeniorProject\\Code\\mysite\\tastypie\\resources.py\", line 1890, in obj_get_list\n    base_object_list = self.apply_filters(request, applicable_filters)\n\n  File \"D:\\Study\\SeniorProject\\Code\\mysite\\tastypie\\resources.py\", line 1862, in apply_filters\n    return self.get_object_list(request).filter(**applicable_filters)\n\n  File \"C:\\Python27\\lib\\site-packages\\django\\db\\models\\query.py\", line 624, in filter\n    return self._filter_or_exclude(False, *args, **kwargs)\n\n  File \"C:\\Python27\\lib\\site-packages\\django\\db\\models\\query.py\", line 642, in _filter_or_exclude\n    clone.query.add_q(Q(*args, **kwargs))\n\n  File \"C:\\Python27\\lib\\site-packages\\django\\db\\models\\sql\\query.py\", line 1250, in add_q\n    can_reuse=used_aliases, force_having=force_having)\n\n  File \"C:\\Python27\\lib\\site-packages\\django\\db\\models\\sql\\query.py\", line 1122, in add_filter\n    process_extras=process_extras)\n\n  File \"C:\\Python27\\lib\\site-packages\\django\\db\\models\\sql\\query.py\", line 1316, in setup_joins\n    \"Choices are: %s\" % (name, \", \".join(names)))\n\nFieldError: Cannot resolve keyword 'page_set' into field. Choices are: displayName, facebook, faculty, friend, id, major, n_id, name, page, password, picture, reporter, year_in_school\n"}
4

2 に答える 2

1

setup_joins からスローされた同じ FieldError にも苦労しており、問題を解決したと思います。多対多の関係をフィルタリングしようとしていましたが、「_set」を機能させることができませんでしたfields.ToManyField()。エラー ケースと機能する単純なフィルターで TastyPie コードをデバッグした後、中間リソースの必要性を完全にバイパスできる可能性があることに気付きました。

これが私にとってうまくいったことです。あなたの状況に役立つことを願っています。モデルの設定がわからないので、同様の例を作成します。

まず、モデル:

### models.py ###
from django.db import models

class Ingredient(models.Model):
    name = models.CharField(max_length=100)
    description = models.TextField()

    def __unicode__(self):
        return '%s' % (self.name)

class RecipeIngredient(models.Model):
    recipe = models.ForeignKey('Recipe')
    ingredient = models.ForeignKey('Ingredient')
    weight = models.IntegerField(null = True, blank = True)

    def __unicode__(self):
        return '%s: %s' % (self.recipe, self.ingredient)

class Recipe(models.Model):
    title = models.CharField(max_length=100)
    ingredients = models.ManyToManyField(Ingredient, through='RecipeIngredient')

    def __unicode__(self):
        return '%s' % (self.title)

ModelResources は次のとおりです。

### api.py ###
from tastypie.resources import ModelResource, ALL, ALL_WITH_RELATIONS
from tastypie import fields
from some_app.models import Ingredient, Recipe

class IngredientResource(ModelResource):

    class Meta:
        queryset = Ingredient.objects.all()
        resource_name = 'ingredient'
        filtering = {
            'name': ALL,
        }

class RecipeResource(ModelResource):
    ingredients = fields.ToManyField(
        'some_app.api.IngredientResource',
        'ingredients',
        full=True)

    class Meta:
        queryset = Recipe.objects.all()
        resource_name = 'recipe'
        filtering = {
            'title': ALL,
            'ingredients': ALL_WITH_RELATIONS,
        }

RecipeIngredientResource がないことに注意してください。IngredientResource に直接フックします。これは、Recipe モデルにingredientsオプション付きの ManyToManyField が含まれているため機能します。through='RecipeIngredient'

特定の材料のすべてのレシピをフィルタリングする URL の例は次のようになります。

http://localhost:8000/api/recipes/recipe/?ingredients__name=blueberry

そして、完全を期すために、この例を実装したい人のためにデータを入力する時間を節約するための「some_app」という名前の Django アプリのフィクスチャを次に示します。

[
    {
        "pk": 1, 
        "model": "some_app.ingredient", 
        "fields": {
            "name": "apple", 
            "description": "a tempting fruit"
        }
    }, 
    {
        "pk": 2, 
        "model": "some_app.ingredient", 
        "fields": {
            "name": "cherry", 
            "description": "a red fruit"
        }
    }, 
    {
        "pk": 3, 
        "model": "some_app.ingredient", 
        "fields": {
            "name": "blueberry", 
            "description": "a blue fruit"
        }
    }, 
    {
        "pk": 4, 
        "model": "some_app.ingredient", 
        "fields": {
            "name": "flour", 
            "description": "used for baking and stuff"
        }
    }, 
    {
        "pk": 5, 
        "model": "some_app.ingredient", 
        "fields": {
            "name": "sugar", 
            "description": "makes stuff sweet"
        }
    }, 
    {
        "pk": 1, 
        "model": "some_app.recipeingredient", 
        "fields": {
            "recipe": 1, 
            "weight": 3, 
            "ingredient": 1
        }
    }, 
    {
        "pk": 2, 
        "model": "some_app.recipeingredient", 
        "fields": {
            "recipe": 1, 
            "weight": 2, 
            "ingredient": 4
        }
    }, 
    {
        "pk": 3, 
        "model": "some_app.recipeingredient", 
        "fields": {
            "recipe": 1, 
            "weight": 4, 
            "ingredient": 5
        }
    }, 
    {
        "pk": 4, 
        "model": "some_app.recipeingredient", 
        "fields": {
            "recipe": 2, 
            "weight": 8, 
            "ingredient": 2
        }
    }, 
    {
        "pk": 5, 
        "model": "some_app.recipeingredient", 
        "fields": {
            "recipe": 2, 
            "weight": 4, 
            "ingredient": 4
        }
    }, 
    {
        "pk": 6, 
        "model": "some_app.recipeingredient", 
        "fields": {
            "recipe": 2, 
            "weight": 6, 
            "ingredient": 5
        }
    }, 
    {
        "pk": 7, 
        "model": "some_app.recipeingredient", 
        "fields": {
            "recipe": 3, 
            "weight": 15, 
            "ingredient": 3
        }
    }, 
    {
        "pk": 8, 
        "model": "some_app.recipeingredient", 
        "fields": {
            "recipe": 3, 
            "weight": 5, 
            "ingredient": 4
        }
    }, 
    {
        "pk": 9, 
        "model": "some_app.recipeingredient", 
        "fields": {
            "recipe": 3, 
            "weight": 6, 
            "ingredient": 5
        }
    }, 
    {
        "pk": 1, 
        "model": "some_app.recipe", 
        "fields": {
            "title": "Apple Pie"
        }
    }, 
    {
        "pk": 2, 
        "model": "some_app.recipe", 
        "fields": {
            "title": "Cherry Pie"
        }
    }, 
    {
        "pk": 3, 
        "model": "some_app.recipe", 
        "fields": {
            "title": "Blueberry Pie"
        }
    }
]
于 2013-02-07T20:19:25.170 に答える
-1

これは、django tastypiedjango と非常によく似た動作をする場合 (誰もが予想できるように)、不適切なキーワードを使用しているということpage_setですpage

フィールド名に複数形を使用することをお勧めします

pages = fields.ToManyField('chula.api.PageResource','page_set',null=True)

つまり、前方の関係はpagesで後方の関係はpage_setどちらなのか思い出せません。しかし、とにかく見た目が良くなります。

于 2013-02-02T16:24:20.223 に答える