我刚刚开始使用django tastypie,而且我很喜欢它.我的问题:我正在搜索与管理视图中相同的功能:为foreignkey字段指定在其他对象的列表响应中看到的内容以及详细响应中的内容.
让我们说这是我简化的模型:
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)
Run Code Online (Sandbox Code Playgroud)
这些是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()
Run Code Online (Sandbox Code Playgroud)
在TimeseriesResource中,如果你使用full=False,你只需要一个带有id的url,如果你使用,full=True你将获得所有信息.实际上,该位置有更多的信息.我需要比full ='False'更多的信息,但不是所有信息都使用full=True.我不想使用exclude选项,因为我没有详细信息或Location对象列表中的信息.
我正在考虑的其中一个选项是为同一个对象制作2个资源,但这并不是最好的解决方案(但我想它会起作用).顺便说一句:我想过这个选项,不会工作(当然),更好地使用bmihelac(谢谢)答案中的解决方法.
虽然...尝试解决方法......引出了一个新问题,请参阅:
没有为不同领域的功能要求show,并index与一些讨论如何可以实现alnong:
https://github.com/toastdriven/django-tastypie/issues/18
在此功能未包含之前,您可以帮助解决此问题:
https://github.com/toastdriven/django-tastypie/issues/18#issuecomment-2695447
好消息!这已被添加到tastypie中.
如果您希望将字段设置为仅在某些页面上,只需将该use_in属性定义为"全部","列表"或"详细信息".
更多信息在这里,但我将为后代提供一个例子:
class DocumentResource(ModelResource):
my_field = fields.CharField(attribute='my_field', use_in='detail')
Run Code Online (Sandbox Code Playgroud)
就那么简单!
| 归档时间: |
|
| 查看次数: |
2774 次 |
| 最近记录: |