[英]Django API tastypie and FK POST
我正在使用好吃的東西,並且創建了我的第一個資源。 但是,我該如何執行以下操作:
用戶名是在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)
只要您的POST包含創建User對象所需的完整信息,它就應該自動創建。 如果不是,則可以使用obj_create
方法遍歷JSON並手動創建用戶對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.