1
我有以下型號獲得來自API調用用戶在Django tastypie創建一個對象
from django.contrib.auth.models import User
...
class Experiment(models.Model):
""" Experiment class """
date = models.DateTimeField(auto_now=True, auto_now_add=True)
user = models.ForeignKey(User)
和下面的資源
class UserResource(ModelResource):
class Meta:
queryset = User.objects.all()
resource_name = 'user'
authorization = DjangoAuthorization()
excludes = ['id', ]
allowed_methods = ['get', ]
class ExperimentResource(ModelResource):
user = fields.ToOneField(UserResource, 'user')
class Meta:
queryset = Experiment.objects.all()
resource_name = 'experiment'
authentication = ApiKeyAuthentication()
authorization = DjangoAuthorization()
excludes = ['id', ]
def hydrate_user(self, bundle):
bundle.obj.user = bundle.request.user
return bundle
但是,當我使用POST請求和ApiKeyAuthentication創建新實驗用戶字段保持空白,即使我爲用戶字段提供保溼。
我使用的Django tastypie 0.11
我實施了你提出的建議,但沒有奏效。我不會在請求中發送用戶模型,因爲我希望最終用戶無權修改該字段。我試圖做一些像[這第一個技巧](http://blog.gingerlime.com/2012/keep-your-hands-off-my-tastypie/),但我認爲這是行不通的,因爲我使用ApiKeyAertificate 。 – jmbarrios