TastyPie:如何发布中间版ManyToMany'通过'资源

时间:2015-02-17 18:00:58

标签: python django tastypie

我有2个模型(RiderProfileRide),它们具有通过中间模型(RideMembership)管理的多对多关系。

我希望能够将新的关系条目发布到我的中级ModelResource,但是我收到一条错误,告诉我我的资源提供的URL无效。

注意 another question与此类似,但它不提供POST数据。

以下是我的模特:

class RiderProfile(models.Model):
    user = models.OneToOneField(User)
    age = models.IntegerField(max_length=2, null=True, blank=True)
    rides = models.ManyToManyField('riderapp.Ride', through="RideMembership", null=True, blank=True)

    def __unicode__(self):
        return self.user.get_username()

class Ride(models.Model):
    name = models.CharField(max_length=64)
    riders = models.ManyToManyField(RiderProfile, through="RideMembership", null=True, blank=True)

    def __unicode__(self):
        return self.name

class RideMembership(models.Model):
    rider = models.ForeignKey(RiderProfile)
    ride = models.ForeignKey(Ride)
    date_joined = models.DateField(default=datetime.now)
    invite_reason = models.CharField(max_length=64)

    def __unicode__(self):
        return self.rider.user.get_username() + ' to ' + self.ride.name()

这是我的资源:

class UserResource(ModelResource):
    ...

class RiderProfileResource(ModelResource):
    class Meta:
        queryset = RiderProfile.objects.all()
        resource_name = 'riders'

    user = fields.ForeignKey(UserResource, 'user', full=True)
    rides = fields.ToManyField('riderapp.api.RideForRiderProfileResource', 'rides', full=True)

class RideResource(ModelResource):
    class Meta:
        queryset = Ride.objects.all()
        resource_name = 'rides'
        authorization = Authorization()
        always_return_data = True

    riders = fields.ToManyField('riderapp.api.RiderProfileForRideResource', 'riders', full=True, blank=True, readonly=True)

class RideMembershipResource(ModelResource):
    class Meta:
        queryset = RideMembership.objects.all()
        resource_name = 'rider_memberships'
        authorization = Authorization()
        always_return_data = True

    rider = fields.ForeignKey(RideForRiderProfileResource, 'rider')
    ride = fields.ForeignKey(RideResource, 'ride')

"""special ride resource for inclusion in a RiderProfile. Omits the `riders` relational field to avoid infinite recursion"""
class RideForRiderProfileResource(ModelResource):
    class Meta:
        queryset = Ride.objects.all()
        resource_name = 'rides_for_riders'
        allowed_methods = [];

""" special rider profile resource for inclusion in a Ride. Omits the `rides` relational field to avoid infinite recursion """
class RiderProfileForRideResource(ModelResource):
    class Meta:
        queryset = RiderProfile.objects.all()
        resource_name = 'riders_for_ride'
        allowed_methods = [];

    user = fields.ForeignKey(UserResource, 'user', full=True)

以下是我尝试通过POST创建新的RideMemebership关系的方式:

网址:http://[...]/api/v1/rider_memberships/

数据:

{
    "rider": "/api/v1/riders/1/",
    "ride": "/api/v1/rides/12/",
    "invite_reason": "because it's my ride!"
}

响应:

{
    "error_message": "An incorrect URL was provided '/api/v1/riders/1/' for the 'RideForRiderProfileResource' resource.",
    "traceback": "Traceback (most recent call last):

  File \"/Library/Python/2.7/site-packages/tastypie/resources.py\", line 201, in wrapper
    response = callback(request, *args, **kwargs)

  File \"/Library/Python/2.7/site-packages/tastypie/resources.py\", line 432, in dispatch_list
    return self.dispatch('list', request, **kwargs)

  File \"/Library/Python/2.7/site-packages/tastypie/resources.py\", line 464, in dispatch
    response = method(request, **kwargs)

  File \"/Library/Python/2.7/site-packages/tastypie/resources.py\", line 1340, in post_list
    updated_bundle = self.obj_create(bundle, **self.remove_api_resource_names(kwargs))

  File \"/Library/Python/2.7/site-packages/tastypie/resources.py\", line 2103, in obj_create
    bundle = self.full_hydrate(bundle)

  File \"/Library/Python/2.7/site-packages/tastypie/resources.py\", line 896, in full_hydrate
    value = field_object.hydrate(bundle)

  File \"/Library/Python/2.7/site-packages/tastypie/fields.py\", line 746, in hydrate
    return self.build_related_resource(value, request=bundle.request)

  File \"/Library/Python/2.7/site-packages/tastypie/fields.py\", line 659, in build_related_resource
    return self.resource_from_uri(self.fk_resource, value, **kwargs)

  File \"/Library/Python/2.7/site-packages/tastypie/fields.py\", line 578, in resource_from_uri
    obj = fk_resource.get_via_uri(uri, request=request)

  File \"/Library/Python/2.7/site-packages/tastypie/resources.py\", line 810, in get_via_uri
    raise NotFound(\"An incorrect URL was provided '%s' for the '%s' resource.\" % (uri, self.__class__.__name__))

NotFound: An incorrect URL was provided '/api/v1/riders/1/' for the 'RideForRiderProfileResource' resource.
"
}

我的POST数据中的两个资源URI都指向有效资源,因此我不确定导致此失败的原因。我正在使用Chrome中的Postman扩展进行测试,如果这很重要的话。我可以发布新的游乐设施,但不是RideMembership条目。

谢谢你们。

更新

正如ozgur指出的那样(请参阅已接受的答案),我在RideMembershipResource中引用了错误的资源。我的ModelResourcesRide模型有多个RiderProfile,因此我可以在我的多对多关系的两边包含每个模型的有限版本。我更新了RideMembershipResource以指向正确的ForgeignKey资源,如下所示:

class RideMembershipResource(ModelResource):
    class Meta:
        queryset = RideMembership.objects.all()
        resource_name = 'rider_memberships'
        authorization = Authorization()
        always_return_data = True

    rider = fields.ForeignKey('riderapp.api.RiderProfileForRideResource', 'rider')
    ride = fields.ForeignKey('riderapp.api.RideForRiderProfileResource', 'ride')

并将我的帖子数据更改为:

{
    "rider": "/api/v1/riders_for_ride/1/",
    "ride": "/api/v1/rides_for_riders/12/",
    "invite_reason": "because it's my ride!"
}

现在我可以发布关系了。干杯!

1 个答案:

答案 0 :(得分:1)

资源RideMembershipResource正在加入两个单独的Ride个实例。

我认为您应该在rider资源中将RideMembershipResource字段更改为以下内容;

rider = fields.ForeignKey(RiderProfileForRideResource, 'rider')

我对 tastypie 了解不多,但您可能需要将POST数据中的resource url更改为:

{
    "rider": "/api/v1/riders_for_ride/1/",
    "ride": "/api/v1/rides/12/",
    "invite_reason": "because it's my ride!"
}