i'm new django , i'm experimenting using tastypie create simple api. can't seem figure out proper way show related information in 1 resource another.
models.py
class tbucket(models.model): name = models.charfield(max_length=50) bucket_type = models.integerfield() bucket_seed = models.decimalfield(null=true, max_digits=10, decimal_places=2) class meta: db_table = u't_bucket' ordering = ['name'] class ttransaction(models.model): bucket = models.foreignkey(tbucket) date = models.datetimefield() amount = models.decimalfield(null=true, max_digits=10, decimal_places=2) account = models.integerfield()
api.py (snippet)
class bucketresource(modelresource): class meta: queryset = tbucket.objects.all() resource_name = 'bucket' include_resource_uri = false allowed_methods = ['get','post','put','delete'] authorization = authorization() class transactionresource(modelresource): bucket_name = fields.toonefield(bucketresource, 'name', null=true) class meta: queryset = ttransaction.objects.all() resource_name = 'transaction' include_resource_uri = false allowed_methods = ['get','post','put','delete'] authorization= authorization()
the records in db this:
t_bucket id:1, name:food, bucket_type:1, bucket_seed:50.00 id:2, name:gas, bucket_type:1, bucket_seed:25.00 t_transaction: id:5, bucket_id:1, date:2013-03-31, amount:8.75, account:1 id:6, bucket_id:2, date:2013-04-01, amount:25.50, account:1
when hit transaction url, see following [{"id": 1, "bucket_name":null, "date":2013-03-31, "amount":8.75, "account":1},{...]
how can tastypie lookup in bucket table "name" , return in json response?
thanks
toonefield display whole bucketresource object, not name. shows null because specified attribute param 'name', while should field in ttransaction model point tbucket model. in case following:
bucket = fields.toonefield(bucketresource, 'bucket', null=true)
but said show whole bucketresource. if need name tbucket model can override dehydrate method:
def dehydrate(self, bundle): bundle.data['bucket_name'] = bundle.obj.bucket.name
and show bucket_name way
Comments
Post a Comment