Search code examples
djangoresttastypiedjango-related-manager

Related Resources with Django and Tastypie


EDIT 2: I have made some progress and have updated my code as follows:

I have these three 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 now the output is like this:

<variable>
 <variableName>...</variableName>
 <variableid>...</variableid>
 <result type="list">
  <object>
    <resultid>...</resultid>
    <units>
      <unitname>...</unitname>
      <unittype>...</unittype>
    </units>
  </object>
</variable>

I'm trying to get the following output when I request this 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 that I have to go through ResultsResource to get to UnitsResource, but how can I have my XML output ignore the things from ResultsResource? As a side question, what documentation could I look into to help me figure this out?


Solution

  • I think I've figured out what I need. This is producing the results I 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 = {}
            for k, v in bundle.data.iteritems():
                if (k != u'results'):
                    variablesBundle[k] = v 
                else:
                    for i in v:        
                        unitsBundle = i.data['units'] 
            variablesBundle['unit'] = unitsBundle
            return variablesBundle