听起来像是一份工作
extra。
A.objects.extra( select={ 'b_count': 'SELECT COUNT(*) FROM yourapp_b WHERe yourapp_b.a_id = yourapp_a.id', }, where=['b_count < 2'])如果B计数通常是您需要的过滤或排序标准,或者需要显示在列表视图中,则可以考虑通过将b_count字段添加到A模型并在添加B或添加B时使用信号对其进行更新来考虑非规范化已删除:
from django.db import connection, transactionfrom django.db.models.signals import post_delete, post_savedef update_b_count(instance, **kwargs): """ Updates the B count for the A related to the given B. """ if not kwargs.get('created', True) or kwargs.get('raw', False): return cursor = connection.cursor() cursor.execute( 'UPDATE yourapp_a SET b_count = (' 'SELECT COUNT(*) FROM yourapp_b ' 'WHERe yourapp_b.a_id = yourapp_a.id' ') ' 'WHERe id = %s', [instance.a_id]) transaction.commit_unless_managed()post_save.connect(update_b_count, sender=B)post_delete.connect(update_b_count, sender=B)另一种解决方案是在添加或删除相关的B时管理A对象上的状态标志。
B.objects.create(a=some_a)if some_a.hidden and some_a.b_set.count() > 1: A.objects.filter(id=some_a.id).update(hidden=False)...some_a = b.asome_b.delete()if not some_a.hidden and some_a.b_set.count() < 2: A.objects.filter(id=some_a.id).update(hidden=True)



