Tasty pie and foreign keys

131 views
Skip to first unread message

Twitch

unread,
Jun 6, 2014, 5:49:54 PM6/6/14
to django...@googlegroups.com
Hi,

I need help figuring out how to use foreign keys with tasty pie. 

I'm trying to add a simple json feed to populate a map with points but don't understand how to pull all of the related records in.

I have two models, one for houses and one for inspection reports. I want the json feed to list all of the inspection reports related to the house. 

I've been following the example in the Tasty Pie documentation but not having any luck: 

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):
    house = fields.ToManyField('housing.api.InspectionReport', 'inspectionreport_set', related_name='report')

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

class InspectionReportAPI(ModelResource):
    report = fields.ToOneField(HouseAPI, 'house_inspections')
    
    class Meta:
        queryset = InspectionReport.objects.all() 
        resource_name = 'report'

traceback:
{
    "error_message": "'Options' object has no attribute 'api_name'",
    "traceback": "Traceback (most recent call last):\n\n  File \"/Users/twitch/Projects/project_python/lib/python2.7/site-packages/tastypie/resources.py\", line 201, in wrapper\n    response = callback(request, *args, **kwargs)\n\n  File \"/Users/twitch/Projects/project_python/lib/python2.7/site-packages/tastypie/resources.py\", line 432, in dispatch_list\n    return self.dispatch('list', request, **kwargs)\n\n  File \"/Users/twitch/Projects/project_python/lib/python2.7/site-packages/tastypie/resources.py\", line 464, in dispatch\n    response = method(request, **kwargs)\n\n  File \"/Users/twitch/Projects/project_python/lib/python2.7/site-packages/tastypie/resources.py\", line 1299, in get_list\n    bundles.append(self.full_dehydrate(bundle, for_list=True))\n\n  File \"/Users/twitch/Projects/project_python/lib/python2.7/site-packages/tastypie/resources.py\", line 854, in full_dehydrate\n    bundle.data[field_name] = field_object.dehydrate(bundle, for_list=for_list)\n\n  File \"/Users/twitch/Projects/project_python/lib/python2.7/site-packages/tastypie/fields.py\", line 820, in dehydrate\n    m2m_resource = self.get_related_resource(m2m)\n\n  File \"/Users/twitch/Projects/project_python/lib/python2.7/site-packages/tastypie/fields.py\", line 515, in get_related_resource\n    if related_resource._meta.api_name is None:\n\nAttributeError: 'Options' object has no attribute 'api_name'\n"
}
Reply all
Reply to author
Forward
0 new messages