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