在TastyPie中使用GenericForeignKey创建资源POST

时间:2013-10-30 21:24:56

标签: python django tastypie

TastyPie显示here如何列出 ContentTypes GenericForeignKeys 。我有这个工作,但你怎么做 POST 来创建一个具有通用外键的资源?

这是我的资源:我现在想创建一个新的广告系列,同时同时创建一个新的SingleVoucherReward或MultiVoucherReward,然后再将其链接起来。怎么能在TastyPie中完成?

class CampaignCreateResource(ModelResource):
    """
    API Facet.
    """
    user = fields.ToOneField(UserResource, 'user',  full=True)

    participant_reward = GenericForeignKeyField({
        SingleVoucherReward: SingleVoucherResource,
        MultiVoucherReward: MultiVoucherResource,
    }, 'participant_reward')

    class Meta:
        queryset = Campaign.objects.all()
        resource_name = 'campaign'
        allowed_methods = ['post', 'get']
        authentication = APIAuthentication().get_authentication()
        authorization = UserObjectsOnlyAuthorization()
        validation = FormValidation(form_class=CampaignForm)
        excludes = ['id', 'participant_reward_object_id']

    def hydrate(self, bundle, request=None):
        """
        Tastypie uses a 'hydrate' cycle to take serializated data from the client
        and turn it into something the data model can use.
        """
        bundle.obj.user = get_user_model().objects.get(pk=bundle.request.user.id)
        return bundle

0 个答案:

没有答案