Django tastypie:资源显示与列表请求中的详细请求不同
我刚刚开始使用
django tastypie,而且我很喜欢它.
让我们说这是我简化的模型: 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,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) 这些是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() 在TimeseriesResource中,如果你使用full = False,你只需要一个带有id的url,如果你使用full = True,你将获得所有信息.实际上,该位置有更多的信息. 我正在考虑的其中一个选项是为同一个对象制作2个资源,但这并不是最好的解决方案(但我想它会起作用). 虽然… django-tastypie: Cannot access bundle.request in dehydrate(self,bundle) 解决方法show和index中有不同字段的功能请求,还有一些讨论如何实现它:https://github.com/toastdriven/django-tastypie/issues/18 在此功能未包含之前,您可以帮助解决此问题: https://github.com/toastdriven/django-tastypie/issues/18#issuecomment-2695447 (编辑:甘南站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |