[Answer]-Tastypie – Linking to a "ForeignKey"

1👍

Shouldn’t

libtype = fields.ForeignKey(LibraryTypeResource, 'libtype_id' )

be

libtype = fields.ForeignKey(LibraryTypeResource, 'libtype' )

(without the ‘_id’)

I believe that as it is you are handing the field an int and it is attempting to get the pk from it.

UPDATE:

Missed that libtype_id is an IntegerField, not a ForeignKey (whole point of the question)

Personally I would add a method to the Library to retrieve the LibraryType object. This way you have access to the LibraryType from the Library and you don’t have to override any dehydrate methods.

class Library(models.Model):
    # ... other fields
    libtype_id = models.IntegerField(db_column='libTypeId')

    def get_libtype(self):
        return LibraryType.objects.get(id=self.libtype_id)

.

class LibraryResource(ModelResource):
    libtype = fields.ForeignKey(LibraryTypeResource, 'get_libtype')

Leave a comment