2
class CreateUserResource(ModelResource):
class Meta:
allowed_methods = ['post']
object_class = User
authentication = ApiKeyAuthentication()
authorization = DjangoAuthorization()
include_resource_uri = False
fields = ['username']
models.signals.post_save.connect(create_api_key, sender=User)
def obj_create(self, bundle, request=None, **kwargs):
username, password = bundle.data['username'], bundle.data['password']
#try:
bundle.obj = User.objects.create_user(username, '', password)
#except IntegrityError:
#raise BadRequest('That username already exists')
#return bundle
我正在使用此資源來創建新用戶。但爲了做到這一點,我需要一個api_key。由於我是匿名的,我怎樣才能獲得api_key?如何使用Django和tastypie註冊匿名用戶?
好吧。我編輯了我的代碼。現在我還有一個問題:成功創建新用戶後,新用戶會用哪個api_key發出請求? – Burak 2012-08-02 05:47:02
起初:'user = User.objects.get(id = bundle.obj.id)'有點令人毛骨悚然,'bundle.obj.groups.add(group)'應該很好...... 那麼,首先你應該通過檢查表tastypie_apikey來檢查'models.signals.post_save.connect(create_api_key,sender = User)'是否工作... 創建用戶後,獲取密鑰相當簡單: 'apikey = ApiKey.objects獲得(用戶= bundle.obj)' – Martin 2012-08-02 14:41:09