/ / Tastypie django, fehlende Spalte - django, tastypie

Tastypie django, fehlende Spalte - django, tastypie

Also versuche ich mit meiner tastypie django api zu interagieren. Ich habe die folgenden Modelle in Django erstellt:

#/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)

Und die folgenden Ressourcen:

#/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"])

Jetzt möchte ich die Projektressource mit dem folgenden Befehl auffüllen:

 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/

Aber ich bekomme eine Fehlermeldung als solche:

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):nn  File "/usr/local/lib/python2.7/dist-packages/tastypie/resources.py", line 195, in wrappern
response = callback(request, *args, **kwargs)nn
File "/usr/local/lib/python2.7/dist-packages/tastypie/resources.py", line 426, in dispatch_listn    return self.dispatch("list", request, **kwargs)nn
File "/usr/local/lib/python2.7/dist-packages/tastypie/resources.py", line 458, in dispatchn    response = method(request, **kwargs)nn
File "/usr/local/lib/python2.7/dist-packages/tastypie/resources.py", line 1320, in post_listn    updated_bundle = self.obj_create(bundle, **self.remove_api_resource_names(kwargs))nn
File "/usr/local/lib/python2.7/dist-packages/tastypie/resources.py", line 2084, in obj_createn    return self.save(bundle)nn
File "/usr/local/lib/python2.7/dist-packages/tastypie/resources.py", line 2230, in saven    bundle.obj.save()nn
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 545, in saven    force_update=force_update, update_fields=update_fields)nn
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 573, in save_basen    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)nn
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 654, in _save_tablen    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)nn
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 687, in _do_insertn    using=using, raw=raw)nn
File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py", line 232, in _insertn    return insert_query(self.model, objs, fields, **kwargs)nn
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 1514, in insert_queryn    return query.get_compiler(using=using).execute_sql(return_id)nn  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 903, in execute_sqln    cursor.execute(sql, params)nn
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/util.py", line 69, in executen    return super(CursorDebugWrapper, self).execute(sql, params)nn
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/util.py", line 53, in executen    return self.cursor.execute(sql, params)nn
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)nn
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/util.py", line 53, in executen    return self.cursor.execute(sql, params)nn
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/base.py", line 451, in executen    return Database.Cursor.execute(self, query, params)nnOperationalError:
table photod_project has no column named user_idn"}

Was ist falsch? Warum benötige ich eine Spalte namens user_id? Wie implementiere ich es? Bin ich hier gerade am Ziel? alle Hilfe sehr geschätzt!

Antworten:

1 für die Antwort № 1

Du bist wahrscheinlich gelaufen syncdb vor dem Hinzufügen der user ForeignKey zu deinem Project Modell, so das user_id Spalte (die das darstellt ForeignKey) ist nicht vorhanden.

Wenn Sie sich noch um die Daten in Ihrer Datenbank kümmern und sich nicht darum kümmern, entfernen Sie einfach die Datenbankdatei und führen Sie sie aus syncdb nochmal. Wenn Sie sich um die Daten kümmern, schauen Sie hinein south für eine Datenmigration.

Das Problem hat nichts mit der API zu tun. Wenn Sie den Django-Admin geöffnet haben, erhalten Sie den gleichen Fehler.