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: 
http://django-tastypie.readthedocs.org/en/latest/fields.html#related-name

*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"
}

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/5c7e5695-7f68-4268-a5d6-2392ccdc5b64%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to