So i am trying to interact with my tastypie django api. I have created the following models in django:
#/models.py
from django.db import models
from tastypie.utils.timezone import now
from django.contrib.auth.models import User
class Project(models.Model):
user = models.ForeignKey(User)
name = models.CharField(max_length=200)
pub_date = models.DateTimeField(default=now)
def __unicode__(self):
return self.name
class ProjectImage(models.Model):
Project = models.ForeignKey(Project)
Photo = models.FileField(upload_to = 'images')
PhotoName = models.CharField(max_length=200)
And the following resources:
#/api.py
from tastypie.resources import ModelResource
from photod.models import Project
from tastypie.authorization import DjangoAuthorization,Authorization
from tastypie.authentication import BasicAuthentication
from django.contrib.auth.models import User
from tastypie import fields
from tastypie.serializers import Serializer
# We need a User resource as it is a required field on the project model.
class UserResource(ModelResource):
class Meta:
queryset = User.objects.all()
resource_name = 'user'
excludes = ['email', 'password', 'is_active', 'is_staff', 'is_superuser']
authorization = Authorization()
serializer = Serializer(formats=['json', 'jsonp'])
class ProjectResource(ModelResource):
user = fields.ToOneField( UserResource, 'created_by_user', full = True )
class Meta:
queryset = Project.objects.all()
resource_name = 'project'
authorization = Authorization()
serializer = Serializer(formats=['json', 'jsonp'])
Now i want to populate the project resource with the following command:
curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"name": "The first proj", "pub_date": "2011-05-22T00:46:38", "user": "/api/v1/user/1/"}' http://localhost:1337/api/v1/project/
But i get an error message as such:
HTTP/1.0 500 INTERNAL SERVER ERROR
Date: Wed, 28 May 2014 05:47:29 GMT
Server: WSGIServer/0.1 Python/2.7.3
X-Frame-Options: SAMEORIGIN
Content-Type: application/json
{"error_message": "table photod_project has no column named user_id", "traceback":
"Traceback (most recent call last):\n\n File \"/usr/local/lib/python2.7/dist-packages/tastypie/resources.py\", line 195, in wrapper\n
response = callback(request, *args, **kwargs)\n\n
File \"/usr/local/lib/python2.7/dist-packages/tastypie/resources.py\", line 426, in dispatch_list\n return self.dispatch('list', request, **kwargs)\n\n
File \"/usr/local/lib/python2.7/dist-packages/tastypie/resources.py\", line 458, in dispatch\n response = method(request, **kwargs)\n\n
File \"/usr/local/lib/python2.7/dist-packages/tastypie/resources.py\", line 1320, in post_list\n updated_bundle = self.obj_create(bundle, **self.remove_api_resource_names(kwargs))\n\n
File \"/usr/local/lib/python2.7/dist-packages/tastypie/resources.py\", line 2084, in obj_create\n return self.save(bundle)\n\n
File \"/usr/local/lib/python2.7/dist-packages/tastypie/resources.py\", line 2230, in save\n bundle.obj.save()\n\n
File \"/usr/local/lib/python2.7/dist-packages/django/db/models/base.py\", line 545, in save\n force_update=force_update, update_fields=update_fields)\n\n
File \"/usr/local/lib/python2.7/dist-packages/django/db/models/base.py\", line 573, in save_base\n updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)\n\n
File \"/usr/local/lib/python2.7/dist-packages/django/db/models/base.py\", line 654, in _save_table\n result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)\n\n
File \"/usr/local/lib/python2.7/dist-packages/django/db/models/base.py\", line 687, in _do_insert\n using=using, raw=raw)\n\n
File \"/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py\", line 232, in _insert\n return insert_query(self.model, objs, fields, **kwargs)\n\n
File \"/usr/local/lib/python2.7/dist-packages/django/db/models/query.py\", line 1514, in insert_query\n return query.get_compiler(using=using).execute_sql(return_id)\n\n File \"/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py\", line 903, in execute_sql\n cursor.execute(sql, params)\n\n
File \"/usr/local/lib/python2.7/dist-packages/django/db/backends/util.py\", line 69, in execute\n return super(CursorDebugWrapper, self).execute(sql, params)\n\n
File \"/usr/local/lib/python2.7/dist-packages/django/db/backends/util.py\", line 53, in execute\n return self.cursor.execute(sql, params)\n\n
File \"/usr/local/lib/python2.7/dist-packages/django/db/utils.py\", line 99, in __exit__\n six.reraise(dj_exc_type, dj_exc_value, traceback)\n\n
File \"/usr/local/lib/python2.7/dist-packages/django/db/backends/util.py\", line 53, in execute\n return self.cursor.execute(sql, params)\n\n
File \"/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/base.py\", line 451, in execute\n return Database.Cursor.execute(self, query, params)\n\nOperationalError:
table photod_project has no column named user_id\n"}
What is wrong? Why do i need a colum named user_id? how do i implement it? am I just way of the mark here? all help greatly appreciated!