我有一个父表Image
和子表Resized_image
,所以我已经将images
数据存储在resized_image
表中。我想使用宽高比过滤数据。
这是我的模特
class Image(models.Model):
name=models.CharField(max_length=40,unique=False)
class Resized_image(models.Model):
img = models.ForeignKey(Image, related_name='images', on_delete=models.CASCADE)
image=models.ImageField(upload_to=config.date_format,width_field='width', height_field='height',)
width=models.PositiveIntegerField(null=True, blank=False, editable=False)
height=models.PositiveIntegerField(null=True, blank=False, editable=False)
size=models.IntegerField(null=True, blank=False,editable=False)
status = models.CharField(max_length=30, default='inserted')
class Meta:
db_table = 'resized_images'
def __str__(self):
return self.status
def save(self,force_insert=False, force_update=False, using=None):
if not self.image:
return
super(Resized_image, self).save()
self.size = os.stat(str(self.image)).st_size
super(Resized_image, self).save()
这是我的观点:
class ImagePostAPIView(mixins.CreateModelMixin,generics.ListAPIView):
queryset = Image.objects.all()
serializer_class = imagesSerializer
def get(self, request):
print("inside get views.py")
return super(mixins.CreateModelMixin,self).get(request)
def myfilter(self, queryset, queryparam, queryarg):
param = self.request.query_params.get(queryparam, None)
#print("param",param)
if param is not None:
try:
newarg = {queryarg : int(param)}
#print("newarg",newarg)
queryset = queryset.filter(**newarg)
except ValueError:
pass
return queryset
def get_queryset(self):
queryset = Image.objects.all()
queryset = self.myfilter(queryset, 'minheight', 'images__height__gte')
queryset = self.myfilter(queryset, 'maxheight', 'images__height__lte')
在这里,我正在使用GET
表中存在的minheight
过滤Resized_image
请求中的数据,我想使用宽高比过滤数据,即我想添加{{1} }字段中使用aspect_ratio
,然后过滤数据...我该怎么做?