django - Tastypie - Linking to a "ForeignKey" -
i have 2 legacy models listed below. library.libtype_id
effectively foreign key librarytype when libtype_id > 0. want represent foreignkey resource in tastypie when condition met.
can me out? have seen this i'm not sure it's same thing? much!!
# models.py class librarytype(models.model): id = models.autofield(primary_key=true) name = models.charfield(max_length=96) class library(models.model): id = models.autofield(primary_key=true) name = models.charfield(max_length=255) project = models.foreignkey('project.project', db_column='parent') libtype_id = models.integerfield(db_column='libtypeid')
here api.py
class librarytyperesource(modelresource): class meta: queryset = librarytype.objects.all() resource_name = 'library_type' class libraryresource(modelresource): project = fields.foreignkey(projectresource, 'project') libtype = fields.foreignkey(librarytyperesource, 'libtype_id' ) class meta: queryset = library.objects.all() resource_name = 'library' exclude = ['libtype_id'] def dehydrate_libtype(self, bundle): if getattr(bundle.obj, 'libtype_id', none) != 0: return librarytyperesource.get_detail(id=bundle.obj.libtype_id)
when i'm getting following error on http://0.0.0.0:8001/api/v1/library/?format=json
"error_message": "'long' object has no attribute 'pk'",
shouldn't
libtype = fields.foreignkey(librarytyperesource, 'libtype_id' )
be
libtype = fields.foreignkey(librarytyperesource, 'libtype' )
(without '_id')
i believe handing field int
, attempting pk
it.
update:
missed libtype_id
integerfield
, not foreignkey
(whole point of question)
personally add method library
retrieve librarytype
object. way have access librarytype
library
, don't have override 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')
Comments
Post a Comment