Skip to content

Clarification on depends using related_name vs. related_name_query and empty dependency lists #168

@hayder-mustafa-aswar

Description

@hayder-mustafa-aswar

@jerch I encountered two issues/questions related to the depends configuration. Below is the relevant snippet from my model:

amount_residual = ComputedField(
    MonetaryField(
        currency_field="company_currency",
        verbose_name="Residual Amount",
        editable=False,
        help_text="The residual amount on a journal item expressed in the company currency.",
    ),
    depends=[
        (
            "self",
            [
                "debit",
                "credit",
                "amount_currency",
                "account",
                "currency",
                "company",
            ],
        ),
        (
            "matched_debit",
            [],
        ),
        (
            "matched_credit",
            [],
        ),
    ],
    select_related=["currency", "company"],
    prefetch_related=["matched_debits", "matched_credits"],
    compute=lambda self: self._compute_residual_amounts_and_reconciled(
        "amount_residual"
    ),
)
  1. Difference between "matched_debit" and "matched_debits" in depends
  • What happened:

    Prior to the latest upgrade, my depends block used "matched_debit" (the related_name_query on the reverse FK). After updating, I changed it to "matched_debits" (the usual related_name for the reverse relationship).

    As soon as I switched to "matched_debits", profiling revealed a large number of unexpected JOINs/queries and a significant slowdown when saving or updating AccountMoveLine records.

    Reverting back to "matched_debit" (i.e., the related_name_query) immediately fixed the performance issue.

  • Questions:

    What is the semantic difference between using a related_name_query (e.g. "matched_debit") versus a direct related_name (e.g. "matched_debits") in the depends list?

    Why does switching to "matched_debits" (the “normal” related name) cause additional queries or JOINs, whereas "matched_debit" (the query-optimized reverse name) does not?

  1. Using an empty list for related‐field dependencies
    In my depends block, I wrote:
(
    "matched_credit",
    [],
),

I wanted this to mean “whenever any related credit move line is added/removed/updated, recompute amount_residual, but I don’t actually depend on any specific field of matched_credit.”

  • Question:

    Is it correct to specify a related field with an empty list ([]) when I only want to trigger the computation on any change to that related object, without reading a particular attribute?

    If not, what is the recommended way to express “depend on any change to the matched_credit reverse relationship, without listing individual fields”?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions