使用Tasty Pie的Django简单API

时间:2014-06-06 15:48:24

标签: python django tastypie

Django的新手,试图创建一个简单的API来为地图构建JSON,但却坚持使用外键。

我做了一个可怕的尝试,但到目前为止只是出现了一个错误:

 "error_message": "'unicode' object has no attribute 'all'"

Models.py:

from django.db import models

class HouseInformation(models.Model):
    house_name = models.CharField(max_length=200, unique=True)
    house_type = models.CharField(max_length=40)
    address = models.CharField(max_length=200)
    latitude = models.CharField(max_length=200)
    longitude = models.CharField(max_length=200)

    class Meta:
        ordering  = ['house_name']

    def __unicode__(self):
        return self.house_name + ', ' + self.house_type + ', ' + self.address

class InspectionReport(models.Model):
    house_name = models.ForeignKey(HouseInformation, to_field='house_name')
    pass_inspection = models.NullBooleanField()
    inspection_date = models.DateField(null=True, blank=True)
    inspection_violations = models.IntegerField(null=True, blank=True)
    file_name = models.CharField(max_length=40, default='none')

api.py:

from tastypie.resources import ModelResource
from tastypie import fields
from housing.models import HouseInformation, InspectionReport


class HouseAPI(ModelResource):
    # fields.ToManyField('APP.api.RelatedResource', 'related name')
    inspection_reports = fields.ToManyField('housing.api.InspectionAPI', 'house_name')

    class Meta:
        resource_name = 'house_inspections'
        queryset = HouseInformation.objects.all()

class InspectionAPI(ModelResource):
    house_information = fields.ToOneField(HouseAPI, 'house_name')

    class Meta:
        queryset = InspectionReport.objects.all()
        resource_name = 'inspection_reports'

urls.py:

from django.conf.urls import *
from housing.api import HouseAPI
from housing import views

housing_api = HouseAPI()

urlpatterns = patterns('',
        url(r'^$', views.index, name='index'),
        (r'^api/', include(housing_api.urls)),
)

更新

测试网址:http://localhost:8080/housing/api/house_inspections/?format=json

完全回溯:

  

{" error_message":"' unicode'对象没有属性' all'",
  "追溯":"追溯(最近一次呼叫最后一次):\ n \ n文件   \" /Users/twitch/Projects/project_python/lib/python2.7/site-packages/tastypie/resources.py \&#34 ;,   第201行,在wrapper \ n response = callback(request,* args,   ** kwargs)\ n \ n File \" /Users/twitch/Projects/project_python/lib/python2.7/site-packages/tastypie/resources.py \",   第432行,在dispatch_list \ n中返回self.dispatch(' list',request,   ** kwargs)\ n \ n File \" /Users/twitch/Projects/project_python/lib/python2.7/site-packages/tastypie/resources.py \",   第464行,在dispatch \ n response = method(request,** kwargs)\ n \ n   文件   \" /Users/twitch/Projects/project_python/lib/python2.7/site-packages/tastypie/resources.py \&#34 ;,   第1299行,在get_list \ n bundles.append中(self.full_dehydrate(bundle,   for_list = True))\ n \ n文件   \" /Users/twitch/Projects/project_python/lib/python2.7/site-packages/tastypie/resources.py \&#34 ;,   第854行,在full_dehydrate \ n bundle.data [field_name] =   field_object.dehydrate(bundle,for_list = for_list)\ n \ n文件   \" /Users/twitch/Projects/project_python/lib/python2.7/site-packages/tastypie/fields.py \&#34 ;,   第819行,脱水\ n为m2m in   the_m2ms.all():\ n \ nAttributeError:' unicode'对象没有属性   '所有' \ n" }

0 个答案:

没有答案