Jeremy D
Jeremy D

Reputation: 4855

Post request to create new custom users in Django

I have a model where I extend the User model:

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

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

I create also resources for my 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()

When I try to create a new user by creatin a POST request, using curl, I got the following:

 ->curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"email": "[email protected]", "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"}

What am I doing wrong? I try to have a setup as simple as possible. I know the issue is coming from the OneToOneField keyword, but I don't know how to fix. I checked many different solutions and didn't find any working for me.

Any hints?

Upvotes: 1

Views: 692

Answers (2)

Jeremy D
Jeremy D

Reputation: 4855

I finally decide to create my own user, without relying on the User model. I have optimal control on my model which makes me feel more confident about what I do.

Nevertheless Thomas' solution is totally correct. I just didn't want to have to do two POST request to create one tuple in my database.

Upvotes: 0

Thomas Orozco
Thomas Orozco

Reputation: 55263

Your data needs to contain the User you'd like to attach your ReaderUser to.

If I remember correctly, you should submit the full object representation of the user, as would be returned if you did e.g.: curl http://localhost:8000/api/user/1

curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"email": "[email protected]", "password": "", "user": {"blah"}}' http://localhost:8000/api/newuser

If you don't have any User though, you'll need to create one first. You can of course do this with another API call.

Upvotes: 1

Related Questions