发布在Django中创建新自定义用户的请求

时间:2013-09-03 21:29:22

标签: django tastypie

我有一个扩展User模型的模型:

class ReaderUser(models.Model):
        user = models.OneToOneField(User)
        email = models.EmailField()                                                                     

        def __unicode__(self):                                                                          
          return self.user.first_name + ',' + str(self.email)

我还为我的tastypie API创建资源:

class CreateReaderUserResource(ModelResource):
    user = fields.OneToOneField('UserResource', 'user', full=False)                                 

    class Meta:
        allowed_methods = ['post']                                                                  
        always_return_data = True
        authentication = Authentication()                                                           
        authorization = Authorization()                                                             
        queryset = ReaderUser.objects.all()                                                         
        resource_name = 'newuser'


class ReaderUserResource(ModelResource):
    class Meta:
        queryset = ReaderUser.objects.all()
        allowed_methods = ['get, put, patch']
        resource_name = 'ruser'                                                                     


class UserResource(ModelResource):                                                                  
    raw_password = fields.CharField(attribute=None, readonly=True, null=True,                       
                                      blank=True)                                                     

    class Meta:
        authentication = MultiAuthentication(
            BasicAuthentication(),
            ApiKeyAuthentication())
        authorization = Authorization()

        allowed_methods = ['get', 'patch', 'put']
        always_return_data = True
        queryset = User.objects.all()

当我尝试使用curl通过creatin创建一个新用户时,我得到了以下内容:

 ->curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"email": "test@test.com", "password": "groscaca"}' http://localhost:8000/api/newuser/
HTTP/1.0 404 NOT FOUND
Date: Tue, 03 Sep 2013 21:17:28 GMT
Server: WSGIServer/0.1 Python/2.7.1
Content-Type: application/json

\"/Users/jrm/Documents/Perso/simplereader/env/lib/python2.7/site-packages/django/db/models/fields/related.py\", line 389, in __get__\n    
raise self.field.rel.to.DoesNotExist\n\nDoesNotExist\n"}

我做错了什么?我尝试让设置尽可能简单。我知道问题来自OneToOneField关键字,但我不知道如何修复。我检查了许多不同的解决方案,但没有找到适合我的工作。

任何提示?

2 个答案:

答案 0 :(得分:1)

您的数据需要包含User您希望将ReaderUser附加到的curl http://localhost:8000/api/user/1

如果我没记错的话,你应该提交用户的完整对象表示,如果你做的话就会返回:curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"email": "test@test.com", "password": "", "user": {"blah"}}' http://localhost:8000/api/newuser

User

如果你没有任何{{1}},你需要先创建一个{{1}}。您当然可以使用其他API调用来执行此操作。

答案 1 :(得分:0)

我最终决定创建自己的用户,而不依赖于User模型。 我对我的模型拥有最佳控制权,这使我对自己的工作更有信心。

然而托马斯的解决方案完全正确。我只是不想做两个POST请求在我的数据库中创建一个元组。

相关问题