简体   繁体   English

Django API Deliciouspie和FK POST

[英]Django API tastypie and FK POST

I'm using tastypie and I have created my first resource. 我正在使用好吃的东西,并且创建了我的第一个资源。 But how do I do the following: 但是,我该如何执行以下操作:

username is passed on the URL, on post I would like to 'do stuff' before it is save. 用户名是在URL上传递的,在发布后,我想在保存之前先“做某事”。 does tastypie have a method for this? 好吃的有办法吗?

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']

model: 模型:

 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)

As long as your POST contains the complete information needed to create a User object, it should create automatically. 只要您的POST包含创建User对象所需的完整信息,它就应该自动创建。 If it doesn't, you can use the obj_create method to iterate through the JSON and create the user object manually. 如果不是,则可以使用obj_create方法遍历JSON并手动创建用户对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM