2016-09-20 10 views
1
class ActionResource(ModelResource): 
    #place = fields.ManyToManyField('restaurant.resource.PlaceResource', 'place', full=True, null=True) 
    place = fields.ManyToManyField(PlaceResource, 
           attribute=lambda bundle: PlaceInfo.objects.filter(action=bundle.obj)) 

    class Meta: 
     queryset = ActionInfo.objects.all() 
     resource_name = 'action' 

     filtering = { 
      'place' : ALL_WITH_RELATIONS, 
     } 

class PlaceResource(ModelResource): 
    location = fields.ManyToManyField(PlaceLocationResource, 'location') 
    class Meta: 
     queryset = PlaceInfo.objects.all() 
     resource_name = 'place' 

     filtering = { 
      'id' : ALL, 
     } 

これは私のresource.pyです。私はいくつかのIDと場所をフィルタリングする場合は、このコードでは:アクションIDでtastypie:シーケンスアイテム0:期待される文字列、関数が見つかりました

http://localhost/api/v1/action/?place__id=2&format=json 

私は、唯一の場所を見つけることができ、アクションは場所のためuniqeです。このURLで、私はエラーを取得:

sequence item 0: expected string, function found 

DjangoのモデルはPlaceModelのように見えるがActionModel

http://localhost/api/v1/action/2/?format=json 

を参照して、多対多のフィールドは私の場所

追加を参照して、通常のJSONを与えています

私のDjangoモデル:

class ActionInfo(models.Model): 
    name  = models.ForeignKey(ActionName,  related_name="title", on_delete=models.CASCADE, null=True, blank=True) 

class PlaceInfo(models.Model): 
    name   = models.ForeignKey(PlaceName, related_name="title", on_delete=models 

.CASCADE、ヌル=真) アクション= models.ManyToManyField(ActionInfo、related_name = "アクション"、空白=真)

私はこのように私のリソースをconstractなければならないことを好んました:

class ActionResource(ModelResource): 
    place = fields.ToOneField(PlaceResource, 'place') 

class PlaceResource(ModelResource): 
    location = fields.ManyToManyField(PlaceLocationResource, 
    action = fields.ToManyField('menus.resources.ActionResource', 'action', full=True) 

しかし、そのようなコードで私が取得:

error: "The model '<ActionInfo: name>' has an empty attribute 'place' and doesn't allow a null value." 

解決:

class ActionResource(ModelResource): 
    place = fields.ToManyField(PlaceResource, 'action', null=True) 

今ではとworkes:

http://localhost/api/v1/action/?place=1&format=json 

答えて

0

関係の(自分の機種に応じて、または逆の名前)の名前に等しいActionResource.place.attributeを設定してみてください。

class ActionResource(ModelResource): 
    place = fields.ToOneField(PlaceResource, 'reverse_name_here') 
関連する問題