简体   繁体   English

Django:仅获取具有最大外键数的对象

[英]Django: get only objects with max foreignkey count

The question is quite simple but possibly unsolvable with Django.这个问题很简单,但可能无法用 Django 解决。

For example I have a model例如我有一个 model

class MyModel(models.Model)
   field_a = models.IntegerField()
   field_b = models.CharField()
   field_c = models.ForegnKey(MyOtherModel)

The question is how to select only objects that have a maximal count of relations with MyOtherModel and preferably(almost mandatory) with only a single query set?问题是如何 select 只有与 MyOtherModel 具有最大关系数的对象,并且最好(几乎是强制性的)只有一个查询集?

Lets say, we have 100 entries all together, 50 pcs.比方说,我们总共有 100 个条目,50 个。 point to field_c_id=1, 40 pcs.指向field_c_id=1,40个。 to field_c_id=2 and rest 10 pcs.到 field_c_id=2 和 rest 10 个。 entries to field_c_id = 3. I need only those which point to field_c_id=1? field_c_id = 3 的条目。我只需要那些指向 field_c_id = 1 的条目? as 50 would be maximal count.因为 50 将是最大计数。

Thanks...谢谢...

Ok first you need a related_name on your MyModel model好的,首先你的 MyModel 需要一个 related_name model

in models.py在模型.py

class MyOtherModel(models.Model)
   field_a = models.IntegerField()


class MyModel(models.Model)
   field_a = models.IntegerField()
   field_b = models.CharField()
   field_c = models.ForeignKey(MyOtherModel, on_delete=models.CASCADE, related_name="my_other_model")

Than you can get most used MyOtherModel比你能得到最常用的 MyOtherModel

in views.py在 views.py 中


most_used = MyOtherModel.objects.annotate(my_other_model_count=Count('my_other_model')).order_by('-my_other_model_count')[:1]

put [:1] if you need 1 if you need more you can set any quantity or remove it put [:1] 如果你需要 1 如果你需要更多你可以设置任何数量或删除它

Or要么

And here is another solution which is better than this这是另一个比这个更好的解决方案

First we have to add one field to your MyOtherModel which keeps count of MyModel首先,我们必须向您的 MyOtherModel 添加一个字段,以保持对 MyModel 的计数

class MyOtherModel(models.Model)
   field_a = models.IntegerField()
   my_model_count = models.IntegerField()

And we have to update count when you add, update or delete object to MyModel for that I recommend to use django signals当您向 MyModel 添加、更新或删除 object 时,我们必须更新计数,因为我建议使用 django 信号

in your models.py在你的 models.py 中

from django.dispatch import receiver
from django.db.models.signals import pre_save, pre_delete


#at the bottom of your model

@receiver(pre_save, sender=MyModel)
def products_reputation_count(sender, instance, *args, **kwargs):
    if instance.pk:
        old_instance = MyOtherModel.objects.get(id=instance.pk)
        old_instance.reputation.product_count -= 1
        instance.reputation.product_count += 1
    else:
        instance.reputation.product_count += 1


@receiver(pre_save, sender= MyModel)
def products_reputation_count(sender, instance, *args, **kwargs):
    instance.reputation.product_count += 1

and in your views.py在你的 views.py 中

most_used = MyOtherModel.objects.order_by("-my_model_count")[:1]

I think this would be helpful.我认为这会有所帮助。 If you have any question related this answer feel free to ask more.如果您有任何与此答案相关的问题,请随时提出更多问题。 Have a good day.祝你有美好的一天。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM