Django tastypie:资源显示在列表请求中的详细请求中不同 [英] Django tastypie: Resource show different in detailed request as in list request

查看:127
本文介绍了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 = default = 0.0)
description = models.CharField(max_length = 256,blank = True)
shortname = models.CharField(max_length = 256,blank = True)
tooltiptext = models.CharField
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):
返回'%s'%(self.name)

类变量(models.Model):
缩写= models.CharField(max_length = 64,unique = True)
名称= 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)'% locationkey.name,self.variablekey.name,self.tstypekey.name)

这些是我的资源对于api:

  class LocationResource(ModelResource):
class Meta:
queryset = Location.objects .all()
resource_name ='location'
excludes = ['public_anonymous','p ublic_authorized']
authentication = BasicAuthentication()
authorization = DjangoAuthorization()

class VariableResource(ModelResource):
class Meta:
queryset = Variable。 object.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 ,你会得到所有的信息。实际上,这个位置有更多的信息。
我需要更多的信息,而不是full ='False',但不是所有使用 full = True 的信息。
我不想使用排除选项,因为我没有详细信息或位置对象的列表hiselve。



我正在考虑的其中一个选择,正在为同一个对象制作2个资源,但这并不是最好的解决方案(但我猜想它会奏效)。
顺便说一下:我想到这个选项,不行(当然),更好地使用在bmihelac的答案中所使用的解决方法(谢谢)。



虽然...
尝试解决方法...
导致我一个新的问题,请参阅:



django-tastypie:无法访问bund.request in dehydrate(self,bundle)

解决方案

对于 show index 中的不同字段有一个功能请求,一些讨论如何可以实施:



https ://github.com/toastdriven/django-tastypie/issues/18



直到此功能不包含在内,也许您可​​以帮助解决此问题:



https: //github.com/toastdri ven / django-tastypie / issues / 18#issuecomment-2695447


I Am just starting with django tastypie, and I Am enthousiastic about it. My question: I Am searching for the same feature as in the admin view: specify for foreignkey fields what to see in the list response of other objects and what in the detailed response.

let's say this is my simplyfied 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 the 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 the TimeseriesResource if you use full=False, you just get an url with an id, if you use full=True you get all the information. In real the location has a lot more information. I need just a bit more information than full='False' but not all the information using full=True. I do not want to use the exclude option, because than I do not have the information in the detailed information or in the list of the Location object itselve.

one of the options I was thinking about, is making 2 resources for the same object, but this doesn't feel like the best solution (but I guess it will work). By the way: I thought over this option, will not work (ofcourse), better use the workaround usggested in the answer of bmihelac (thanks).

Although... trying the workaround... leads me to a new question, see:

django-tastypie: Cannot access bundle.request in dehydrate(self,bundle)

解决方案

There is a feature request for different fields in show and index, alnong with some discussion how it could be implemented:

https://github.com/toastdriven/django-tastypie/issues/18

Until this feature is not included, maybe you can help with this workaround:

https://github.com/toastdriven/django-tastypie/issues/18#issuecomment-2695447

这篇关于Django tastypie:资源显示在列表请求中的详细请求中不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆