我需要根据其他字段的值排除字段。他们之间的关系如下。
class Moo:
...
class Too:
moo = models.ForeignKey(Moo, related_name='moo_too')
...
class PooToo:
moo = models.ForeignKey(Moo)
stature = models.PositiveSmallIntegerField(..)
...
class PooMooResource(ModelResource):
moo = ToOneField(StandAloneMooResource, 'moo', full=True)
class Meta:
list_allowed_methods = ['get']
queryset = PooMoo.objects.select_related('moo').all()
class StandAloneMooResource(ModelResource):
too = ToManyField(TooResource,...)
class Meta:
queryset = Moo.objects.all()
现在我想在too
时公开API中的stature==0
字段,否则不公开。我可以使用use_in
来解决此问题,但问题是stature
StandAloneMooResource
的值
答案 0 :(得分:1)
尝试这样的事情:
class StandAloneMooResource(ModelResource):
too = ToManyField(TooResource,...)
class Meta:
queryset = Moo.objects.all()
def dehydrate_too(self, bundle):
# if this method is run while dehdyrating PooMooResource.moo,
# related_obj should be the related PooMoo
if bundle.related_obj and bundle.related_obj.stature != 0:
return None
return bundle.data['too']
不幸的是,'以及' key仍然存在,而tastypie仍将继续从数据库中检索教程。
这可能会更好:
def foo(bundle):
if bundle.related_obj and bundle.related_obj.stature != 0:
return None
return bundle.obj.tutorials.all()
class StandAloneMooResource(ModelResource):
too = ToManyField(TooResource, attribute=foo)
class Meta:
queryset = Moo.objects.all()
def dehydrate(self, bundle):
# to remove the 'tutorials' key
if 'too' in bundle.data and not bundle.data['too']:
del bundle.data['too']
return bundle
如果bundle.related_obj
无法正常工作:
def foo(bundle):
poomoo = None
try:
# assumes PooMoo.user a relation to User
poomoo = PooMoo.objects.filter(moo=bundle.obj, user=bundle.request.user)[0]
except IndexError:
pass
if poomoo and poomoo.stature != 0:
return None
return bundle.obj.too.all()
答案 1 :(得分:0)
您可以执行以下操作:
.container > div {
/* block properties become available, but only takes the designated width */
display: inline-block;
/* this tells it to include padding & borders when calculating width */
box-sizing: border-box;
width: 30%;
padding: 15px;
/* min / max width ensure it's always within a "reasonable" width window.
This means at responsive it can / will reduce to 2 side-by-side,
and at larger sizes would increase to 3 or 4 side-by-side */
max-width: 250px;
min-width: 200px;
}
.container > div img {
/* ensure the image takes the full width of the space */
width: 100%;
/* this ensures the image is always proportioned properly */
height: auto;
}
.container div.text {
/* this is the weakness of this layout. To get all the boxes to be the same height, you have to set this to a fixed height. */
height: 200px;
}
您可以查看http://django-tastypie.readthedocs.org/en/latest/resources.html