我有像这样的用户资源
class UserResource(ModelResource):
class Meta:
queryset = User.objects.filter(is_active=True)
resource_name = user
excludes = [ email , password , is_active , is_staff , is_superuser ]
serializer = CamelCaseJSONSerializer(formats=[ json ])
list_allowed_methods = [ post ]
detail_allowed_methods = [ get , post , put , patch ]
authentication = ApiKeyAuthentication()
models.signals.post_save.connect(create_api_key, sender=User)
def obj_create(self, bundle, request=None, **kwargs):
try:
bundle = super(UserResource, self).obj_create(bundle, request, **kwargs)
bundle.obj.set_password(bundle.gata.get( password ))
bundle.obj.save()
except IntegrityError:
raise BadRequest( The username already exists )
return bundle
def apply_authorization_limits(self, request, object_list):
return object_list.filter(user = request.user)
我用睡梦来创造新用户 使用这个代码线
import slumber
>>> api = slumber.API("http://127.0.0.1:8000/api/v1")
>>> new = api.user.post({"firstName" : "fname", "lastName" : "lname", "username" : "anewuser", "password" : "123456", "email" : "[email protected]"})
我有一个错误,说
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Python/2.7/site-packages/slumber/__init__.py", line 125, in post
resp = self._request("POST", data=s.dumps(data), params=kwargs)
File "/Library/Python/2.7/site-packages/slumber/__init__.py", line 104, in _request
raise exceptions.HttpClientError("Client Error %s: %s" % (resp.status_code, url), response=resp, content=resp.content)
slumber.exceptions.HttpClientError: Client Error 401: http://127.0.0.1:8000/api/v1/user/
我不认为我应该得到 401 创建新用户...
我不想让 API 被打开, 这样所有用户的列表可以重新编造, 我只想让用户登录在用户中, 以获取他们的信息
我做错什么了?
< gener > 更好的方法 - 创建一个用于创建用户的新资源。 此资源只能被超级用户 generate > 调用
class UserSignUpResource(ModelResource):
class Meta:
object_class = User
queryset = User.objects.all()
allowed_methods = [ post ]
include_resource_uri = False
resource_name = newuser
excludes = [ is_active , is_staff , is_superuser ]
serializer = CamelCaseJSONSerializer(formats=[ json ])
authentication = ApiKeyAuthentication()
authorization = DjangoAuthorization()
models.signals.post_save.connect(create_api_key, sender=User)
def obj_create(self, bundle, request=None, **kwargs):
try:
bundle = super(UserSignUpResource, self).obj_create(bundle, request, **kwargs)
bundle.obj.set_password(bundle.data.get( password ))
bundle.obj.save()
except IntegrityError:
raise BadRequest( The username already exists )
return bundle
def apply_authorization_limits(self, request, object_list):
return object_list.filter(id=request.user.id, is_superuser=True)
以此命令创建新用户
curl -v -X POST -d {"username" : "username", "password" : "123456"} -H "Authorization: ApiKey superusername:apikey" -H "Content-Type: application/json" http://127.0.0.1:8000/api/v1/newuser/