I am creating a Tastypie API for my Django project. I have a models in django models.py like this:
class User(models.Model):
nick = models.CharField(max_length = 255)
email = models.CharField(max_length = 511)
password = models.CharField(max_length = 63)
reg_date = models.DateTimeField('register date')
od_user = models.CharField(max_length = 1024)
def __unicode__(self):
aux = self.nick + " " + self.email
return aux
and I also have a ModelResource for my Tastypie API like this:
class UserResource(ModelResource):
class Meta:
queryset = User.objects.all()
resource_name = 'user'
excludes = ['password']
allowed_methods = ['get', 'post', 'put', 'delete']
authorization = Authorization()
always_return_data=True
def obj_create(self, bundle, request=None, **kwargs):
username, password = bundle.data['nick'], bundle.data['password']
try:
bundle.obj = User(nick, "email@test", password,timezone.now(),"od_test")
bundle.obj.save()
except IntegrityError:
raise BadRequest('That username already exists')
return bundle
but this doesn’t work. I’ve looked How to create or register User using django-tastypie API programmatically? but I don’t know how create a user in my database.
I use:
curl -v -H "Content-Type: application/json" -X POST --data '{"nick":"test2", "password":"alparch"}' http://127.0.0.1:8000/api/v1/user/?format=json
to do POST method.
How can I create an object with a Tastypie API?
You can’t create a user with positional arguments in the way you have:
Instead, you must use keyword arguments: