I'm just starting out with django tastypie, and I'm an enthusiast about it. My question is: I'm looking for the same function as in administrator mode: specify for foreignkey fields what to look for in the answer list of other objects and what in the detailed answer.
let's say this is my simple model:
class Location(models.Model): name = models.CharField(max_length=256, blank=True) longitude = models.FloatField(blank=True, default=0.0) latitude = models.FloatField(blank=True, default=0.0) description = models.CharField(max_length=256, blank=True) shortname = models.CharField(max_length=256, blank=True) tooltiptext = models.CharField(max_length=1000, blank=True) locationtype = models.ForeignKey(LocationType, blank=True, null=True) public_anonymous = models.BooleanField(default=False, blank=False, null=False) public_authorized = models.BooleanField(default=False, blank=False, null=False) def __str__(self): return '%s' % (self.name) class Variable(models.Model): abbreviation = models.CharField(max_length=64, unique=True) name = models.CharField(max_length=256, blank=True) unit = models.CharField(max_length=64, blank=True) def __str__(self): return '%s [%s]' % (self.name, self.unit) class Timeseries(models.Model): locationkey = models.ForeignKey(Location) variablekey = models.ForeignKey(Variable) tstypekey = models.ForeignKey(TimeseriesType) def __str__(self): return '%s: %s (%s)' % (self.locationkey.name, self.variablekey.name, self.tstypekey.name)
and these are my resources for api:
class LocationResource(ModelResource): class Meta: queryset = Location.objects.all() resource_name = 'location' excludes = ['public_anonymous', 'public_authorized'] authentication = BasicAuthentication() authorization = DjangoAuthorization() class VariableResource(ModelResource): class Meta: queryset = Variable.objects.all() resource_name = 'variable' authentication = BasicAuthentication() authorization = DjangoAuthorization() class TimeseriesTypeResource(ModelResource): class Meta: queryset = TimeseriesType.objects.all() resource_name = 'timeseriestype' authentication = BasicAuthentication() authorization = DjangoAuthorization() class TimeseriesResource(ModelResource): location = fields.ForeignKey(LocationResource, 'locationkey', full=False) variable = fields.ForeignKey(VariableResource, 'variablekey', full=False) timeseriestype = fields.ForeignKey(TimeseriesTypeResource, 'tstypekey', full=False) class Meta: queryset = Timeseries.objects.all() resource_name = 'timeseries' authentication = BasicAuthentication() authorization = DjangoAuthorization()
In TimeseriesResource, if you use full=False , you just get the url with id, if you use full=True , you get all the information. In a real place there is much more information. I need a little more information than full = 'False', but not all information with full=True . I do not want to use the exclude parameter because I do not have the information in the details or in the list of the Location object.
one of the options I was thinking about is 2 resources for the same object, but that doesn't seem like the best solution (but I think it will work). By the way: I thought about this option, it wonβt work (of course), itβs better to use the workaround laid down in bmihelac's answer (thanks).
Despite ... trying a workaround ... leads me to a new question, see:
django-tastypie: cannot access bundle.request in dehydrate (self, bundle)