rest - Related Resources with Django and Tastypie -
edit 2: have made progress , have updated code follows:
i have these 3 models in models.py:
class variables(models.model): variableid = models.integerfield(db_column='variableid', primary_key=true) variablename = models.charfield(db_column='variablenamecv', max_length=255) class meta: managed = false db_table = 'variables' class results(models.model): resultid = models.bigintegerfield(db_column='resultid', primary_key=true) variable = models.foreignkey('variables', related_name='results', db_column='variableid') units = models.foreignkey('units', related_name='units', db_column='unitsid') class meta: managed = false db_table = 'results' class units(models.model): unitsid = models.integerfield(db_column='unitsid', primary_key=true) unitstype = models.charfield(db_column='unitstypecv', max_length=255) unitsname = models.charfield(db_column='unitsname', max_length=255) class meta: managed = false db_table = 'units'
i have these tastypie model resources defined:
class unitsresource(modelresource): class meta: queryset = units.objects.all() resource_name = 'units' collection_name = 'units' authorization = authorization() serializer = serializer() class resultsresource(modelresource): units = fields.foreignkey(unitsresource, attribute='units', full=true, null=true); class meta: queryset = results.objects.all() resource_name = 'results' collection_name = 'results' authorization = authorization() serializer = serializer() class variablesresource(modelresource): results = fields.tomanyfield(resultsresource, attribute='results', related_name='results', full=true, null=true); class meta: queryset = variables.objects.all() resource_name = 'variables' collection_name = 'variables' authorization = authorization() serializer = serializer()
right output this:
<variable> <variablename>...</variablename> <variableid>...</variableid> <result type="list"> <object> <resultid>...</resultid> <units> <unitname>...</unitname> <unittype>...</unittype> </units> </object> </variable>
i'm trying following output when request url (http://127.0.0.1:8000/api/v1/variables/4/
):
<variable> <variablename>...</variablename> <variableid>...<variableid> <unit> <unitname>...</unitname> <unittype>...</unittype> </unit> </variable>
it apears have go through resultsresource
unitsresource
, how can have xml output ignore things resultsresource
? side question, documentation me figure out?
i think i've figured out need. producing results want:
class variablesresource(modelresource): results = fields.tomanyfield(resultsresource, attribute='results', related_name='results', full=true, null=true); class meta: queryset = variables.objects.all() resource_name = 'variables' collection_name = 'variables' authorization = authorization() serializer = serializer() def dehydrate(self, bundle): variablesbundle = {} unitsbundle = {} k, v in bundle.data.iteritems(): if (k != u'results'): variablesbundle[k] = v else: in v: unitsbundle = i.data['units'] variablesbundle['unit'] = unitsbundle return variablesbundle