0

我正在使用tastepie,并且创建了我的第一个资源。但是我该怎么做:

用户名在 URL 上传递,在帖子上我想在保存之前“做一些事情”。美味派有这个方法吗?

class CommonMeta:
    authentication = ApiKeyAuthentication()
    authorization = UserObjectsOnlyAuthorization()



class SMSResource(ModelResource):
    class Meta(CommonMeta):
        queryset = Batch.objects.all()
        resource_name = 'sms'
        list_allowed_methods = ['get', 'post']
        detail_allowed_methods = ['get']

模型:

 content = models.TextField(validators=[validate_GSM_characters])
    type = models.CharField(max_length=16, choices=TYPES,
                            default="Standard", null=True, blank=True)
    priority = models.CharField(max_length=16, choices=PRIORITIES,
                                default="Normal", null=True, blank=True)
    status = models.CharField(max_length=16, choices=STATUSES,
                              default="Pending", null=True, blank=True)

    created = models.DateTimeField(auto_now_add=True, help_text="Shows when object was created.")
    schedule = models.DateTimeField(blank=True, null=True, help_text="Shows when object was created.")

    #FK
    sender_name = models.ForeignKey(Originator)
    user = models.ForeignKey(User)
4

1 回答 1

1

只要您的 POST 包含创建用户对象所需的完整信息,它就应该自动创建。如果没有,您可以使用该obj_create方法遍历 JSON 并手动创建用户对象。

于 2013-06-12T12:47:57.810 回答