Add a property or a function that will count the total number of pictures for you:
# Calculate non-empty fields count
class Picture(models.Model):
picture1 = models.CharField(help_text="picture 1", max_length=255, blank=True)
picture2 = models.CharField(help_text="picture 2", max_length=255, blank=True)
# save it to the database
non_empty_fields = property(picture_count)
def picture_count(self):
fields = self._meta.get_fields()
n_count = len([ x for x in fields if getattr(self, field.name) ])
return n_count
class Meta:
ordering = ('picture_count', '-id')
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)