Not if you want it to be fast. Python itself is slow - so you should try to
offload most of the computation on the database. So filtering would need to
be done on database fields as well.

Otherwise you would need to load all of the rows into memory and then do
the filtering there.

If you want to continue along the route you have taken now - you probably
could solve it with annotated queries - something like this:
https://stackoverflow.com/questions/1396264/how-to-sort-by-annotated-count-in-a-related-model-in-django

Regards,

Andréas


Den tors 9 feb. 2023 kl 13:21 skrev Chelsea Fan <
allaberdi16yazha...@gmail.com>:

> understood, is there any way to filter objects by method value?
>
> On Thu, Feb 9, 2023 at 4:42 PM Andréas Kühne <andreas.ku...@hypercode.se>
> wrote:
>
>> No.
>>
>> Ordering works by using the database to order the models. It therefore
>> needs to be a database field - otherwise the database can't order by the
>> field?
>>
>> Regards,
>>
>> Andréas
>>
>>
>> Den tors 9 feb. 2023 kl 12:09 skrev Chelsea Fan <
>> allaberdi16yazha...@gmail.com>:
>>
>>> hello guys, Is it possible to use model method value to ordering model
>>> objects in meta class?
>>>
>>> class Post(models.Model):
>>>     title = models.CharField(max_length=255, verbose_name="ady")
>>>     text = RichTextField(verbose_name="text")
>>>     tagList = models.ManyToManyField(Tag, verbose_name="taglar",
>>> related_query_name="tagList")
>>>     image = models.ImageField(upload_to="postImage/", verbose_name=
>>> "surat")
>>>     seen = models.ManyToManyField(UserId,verbose_name="görülen sany",
>>> blank=True, related_name="gorulen")
>>>     like = models.ManyToManyField(UserId,verbose_name="like sany", blank
>>> =True)
>>>     share = models.PositiveIntegerField(verbose_name="paýlaşylan sany",
>>> null=True, blank=True, default="0")
>>>     createdAt = models.DateTimeField(auto_now_add=True, 
>>> verbose_name="goşulan
>>> güni")
>>>
>>>     class Meta:
>>>         verbose_name_plural="Makalalar"
>>>         # ordering = ("-createdAt",)
>>>         ordering = ["-hotness",]
>>>
>>>     def __str__(self):
>>>         return self.title
>>>
>>>     def likes(self):
>>>         return self.like.count()
>>>
>>>     likes.short_description = "Like sany"
>>>     likes.allow_tags = True
>>>
>>>     def seens(self):
>>>         return self.seen.count()
>>>
>>>     seens.short_description = "Görülen sany"
>>>     seens.allow_tags = True
>>>
>>>     @property
>>>     def hotness(self):
>>>         return self.likes() + self.seens() + self.share
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Django users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to django-users+unsubscr...@googlegroups.com.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/django-users/CAJwZndfmes4g2KWUB3Fz6wNRORQ40Fxj_NYwYKWCF6DX96OVyg%40mail.gmail.com
>>> <https://groups.google.com/d/msgid/django-users/CAJwZndfmes4g2KWUB3Fz6wNRORQ40Fxj_NYwYKWCF6DX96OVyg%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to django-users+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/CAK4qSCfpaVQ1YXMbVae26RLgsYYBcLRMcpgQOEm9z3%2B6NpN5Ww%40mail.gmail.com
>> <https://groups.google.com/d/msgid/django-users/CAK4qSCfpaVQ1YXMbVae26RLgsYYBcLRMcpgQOEm9z3%2B6NpN5Ww%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAJwZnddOzyjuawMFe4pZtn4oj8YxZDBX_N1SK492G3RhENyVqg%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAJwZnddOzyjuawMFe4pZtn4oj8YxZDBX_N1SK492G3RhENyVqg%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAK4qSCf02LMA9tN_AEijDnM58PcT1uk8Q0zmr%2BORBc_bZXwSaA%40mail.gmail.com.

Reply via email to