簡體   English   中英

Django對過濾后的QuerySet的所有相關對象求和

[英]Django sum all related objects of a filtered QuerySet

我想在psuedo代碼中做的是:

def import_transaction_deposit_crypto(Importer):
    logger = get_nexchange_logger(__name__, True, True)
    existent_addresses = Address.objects.filter(
        currency__is_crypto=True,
        type=Address.DEPOSIT,
        currency__wallet__in=Importer.RELATED_NODES

    ).tx_to_set.count()

導入器的示例值:

class LitecoinTxImporter:
    RELATED_NODES = 'ltc_rpc_1'

tx_totx_to (反向關系):

class Address(BtcBase, SoftDeletableModel):
    address_to = models.ForeignKey('core.Address',
                                   related_name='txs_to')

我們的想法是計算屬於特定 RPC節點(錢包)的所有“已導入”事務,以便將其提供給listtransactions RPC端點的from參數(一般來說,出於分頁目的)。

這里記錄了一個完美匹配的例子:

# Build an annotated queryset
>>> from django.db.models import Count
>>> q = Book.objects.annotate(Count('authors'))
# Interrogate the first object in the queryset
>>> q[0]
<Book: The Definitive Guide to Django>
>>> q[0].authors__count
2

您可以從Address開始過濾:

Adress.objects.filter(address_to__curency__is_crpyto=True, ...).count()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM