简体   繁体   中英

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. 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. If it doesn't, you can use the obj_create method to iterate through the JSON and create the user object manually.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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