django model method

107 views
Skip to first unread message

Chelsea Fan

unread,
Feb 9, 2023, 6:10:15 AM2/9/23
to django...@googlegroups.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

Andréas Kühne

unread,
Feb 9, 2023, 6:43:05 AM2/9/23
to django...@googlegroups.com
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


--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAJwZndfmes4g2KWUB3Fz6wNRORQ40Fxj_NYwYKWCF6DX96OVyg%40mail.gmail.com.

Chelsea Fan

unread,
Feb 9, 2023, 7:21:24 AM2/9/23
to django...@googlegroups.com
understood, is there any way to filter objects by method value?

Andréas Kühne

unread,
Feb 9, 2023, 8:09:47 AM2/9/23
to django...@googlegroups.com
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


Vitaly Bogomolov

unread,
Feb 9, 2023, 10:45:21 AM2/9/23
to Django users
You can look towards  Django Q-objects

This filtering method is implemented in the MultiChoiceExt filter of my DjangoAdminFilters library for using in Django admin.

For filtering in regular Django views, you can use this code as a starting point.

четверг, 9 февраля 2023 г. в 16:21:24 UTC+4, Chelsea Fan:

Gabriel Araya Garcia

unread,
Feb 10, 2023, 9:02:16 AM2/10/23
to django...@googlegroups.com
Allways there is a way to improve the code, but  if that not run, then try with CYTHON

Gabriel Araya Garcia
GMI - Desarrollo de Sistemas Informáticos




Jason

unread,
Feb 13, 2023, 7:41:33 AM2/13/23
to Django users
there's alot to not like about the implementation here.  You're effectively hiding queries and db hits with those methods, since the naming does not give any indication that its a db hit.  So its really easy to forget that, and then after a while, you're wondering why your project is so slow and executing alot of unnecessary queries

for seen, likes and hotness counts, I'd rather denormalize  and have a count field for all those values.  Then either use a post-save signal to update the values, or use a recurring background task to regenerate the fields on an interval.

Reply all
Reply to author
Forward
0 new messages