Reputation: 21
I configured a simple API and resource, connecting to an existing DB (that's why I needed to specify table names and columns on the model). I can create and list objects normally. But when I create one, I never get the Location header correctly, nor can I return the created data.
Creating an object:
curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"system_key": "test","system_nam": "test","system_url": "https://test/test"}' http://127.0.0.1:7000/api/system/?ticket=TGT-585-d9f9effb36697401dce5efd7fc5b3de4
Response:
HTTP/1.0 201 CREATED
Date: Thu, 14 Feb 2013 18:58:17 GMT
Server: WSGIServer/0.1 Python/2.7.3
Vary: Accept
Content-Type: text/html; charset=utf-8
Location: http://127.0.0.1:7000/api/system/None/
Notice the Location header. It seems like the object is created successfully, but the information is not returning from the DB to the API, so it can be used on the response. For the sake of clarity, the line is perfect on the DB table.
If I add always_return_data = True
I get this:
HTTP/1.0 400 BAD REQUEST
Date: Thu, 14 Feb 2013 19:00:32 GMT
Server: WSGIServer/0.1 Python/2.7.3
Content-Type: application/json
{"error": "The object '<system: MRMteste>' has an empty attribute 'system' and doesn't allow a default or null value."}
My resource and model are incredibly simple:
resource:
class SystemResource(ModelResource):
class Meta:
list_allowed_methods = ['get', 'post']
detail_allowed_methods = ['get', 'put','patch']
queryset = system.objects.all()
resource_name = 'system'
authorization = Authorization()
authentication = customAuth.CustomAuthentication()
def hydrate_system_timestamp(self, bundle):
bundle.data['system_timestamp'] = get_now_time()
return bundle
model:
class system(models.Model):
list_display = ('system_nam')
system = models.IntegerField(primary_key=True, db_column="system_id")
system_nam = models.CharField(max_length=50)
system_key = models.CharField(max_length=255)
system_url = models.CharField(max_length=100)
is_deleted_flg = models.BooleanField()
system_timestamp = models.DateTimeField(default=datetime.now)
def __unicode__(self):
return self.system_nam
class Meta:
db_table = "system"
There is nothing on the documentation about this. Anyone with more experience with tastypie can tell me if the model and resource are correct? I'm using the latest version of tastypie and django as of this date.
thanks a bunch
Upvotes: 1
Views: 963
Reputation: 21
Actually I've solved the problem.
On the model, if you need to define the column name, as I did, don't use IntegerField on a primary key. Do it like this:
system = models.AutoField(primary_key=True, db_column="system_id")
The way tastypie looks after the newly created object, it needs the autoincrement defined on the model, so it knows the last created ID. Good find, hope it helps someone someday.
Upvotes: 1
Reputation: 1040
You have to set in the meta class of the Resource:
always_return_data = True
And try your unique id field to be with default name id not service.
Upvotes: 1