Django的新手,尝试创建一个简单的API来为 Map 构建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'”,“traceback”:“Traceback(最近一次调用最后一次):\ n \ n File \”/ Users / twitch / Projects / project_python / lib / python2.7 / site-packages / tastypie / resources.py \“,第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 File \” /Users/twitch/Projects/project_python/lib/python2.7/site-packages/tastypie/resources.py \”,线1299,在get_list \ n bundles.append(self.full_dehydrate(束,for_list = TRUE))\ n \ n File \“/ Users / twitch / Projects / project_python / lib / python2.7 / site-packages / tastypie / resources.py \”,第854行,在full_dehydrate \ nsundhip.data [field_name] = field_object.dehydrate (捆绑,f or_list = for_list)\ n \ n文件\“/ Users / twitch / Projects / project_python / lib / python2.7 / site-packages / tastypie / fields.py \”,第819行,脱水\ n表示_m2ms中的m2m . all():\ n \ n属性错误:'unicode'对象没有属性'all'\ n“}