English 中文(简体)
使用401年的肉食结果创建新用户
原标题:Create a new user using tastypie results in 401

我有像这样的用户资源

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 > 更好的方法 - 创建一个用于创建用户的新资源。 此资源只能被超级用户 调用

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/
最佳回答

我通过创造新资源解决了它

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)
问题回答

暂无回答




相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签