Horilla CRM stores forecast values for each owner, period, and forecast type. These values include pipeline, best case, commit, closed, and actual amounts or quantities.

Normally, these values stay up to date as opportunities change. However, bulk updates, imports, custom scripts, or missed signals can cause stored forecasts to differ from the current Horilla CRM data.

The recalculate_forecasts management command helps fix these inconsistencies by recalculating existing forecast records using current data. This post explains how to use the command, understand its output, and run it safely.

Usage

python manage.py recalculate_forecasts

When run without options, the command recalculates all forecasts in the database, according to its implementation.

Options

OptionPurpose
–user-id=123Recalculates forecasts for a specific user
–fiscal-year-id=5Recalculates forecasts for a specific fiscal year
–forecast-type-id=2Recalculates forecasts for a specific forecast type
–dry-runShows the changes that would be made without saving them

The filters can be combined. For example, –user-id=123 –fiscal-year-id=5 limits the operation to forecasts matching both the specified user and fiscal year.

What it does

The command is located at horilla_crm/forecast/management/commands/recalculate_forecasts.py.

Its workflow is as follows:

  1. Selects the forecasts. It retrieves forecast records that match the supplied filters and orders them by owner and period number.
  2. Groups the forecasts. It groups the selected records by owner and fiscal year, creating a ForecastCalculator instance for each group.
  3. Recalculates the values. It calls calculate_forecast_values(owner, period, forecast_type) to calculate fresh values for each forecast.
  4. Compares the values. It compares the existing and calculated pipeline, best case, commit, closed, and actual values. For quantity-based forecast types, it compares the corresponding *_quantity fields. For amount-based types, it compares the *_amount fields.
  5. Updates changed records. It saves forecasts only when the calculated values differ from the stored values. If no changes are needed, it prints No changes needed.
  6. Displays a summary. It reports the number of forecasts processed and any errors encountered.

Reading the output

A typical run might look like this:

Starting forecast recalculation…
Found 24 forecasts to recalculate

Processing 2 user/fiscal-year combinations…

Processing forecasts for: Jane Doe (jane@example.com)
Fiscal Year: FY 2026
– Recalculating: Jane Doe – Q1 – Revenue
pipeline: 120000 → 135000
commit: 40000 → 52000
✓ Updated
– Recalculating: Jane Doe – Q2 – Revenue
No changes needed

Each changed value is displayed in the old → new format. This makes it easier to identify discrepancies and understand how the recalculation affects stored forecasts.

The example above is illustrative; the exact output depends on the command’s implementation and the data being processed.

Dry run first

Always start with a dry run when working with production data:

python manage.py recalculate_forecasts –dry-run

A dry run should display the changes that would be made without saving them. In the example output described here, proposed changes are marked as (Would update).

Review the output before running the command without –dry-run. If the proposed changes are unexpected or affect too many records, narrow the scope using the available filters.

When to use it

The recalculate_forecasts command is useful in several situations:

  • After bulk imports or updates: When opportunities are imported or updated without triggering the usual recalculation logic.
  • After changing forecast settings: When stage forecast categories, probabilities, or opportunity amounts are changed through scripts or direct database updates.
  • After restoring or migrating data: When forecast values may no longer match the restored or migrated opportunity records.
  • When forecast values appear incorrect: When a user’s forecast screen does not match the current opportunity data.

For larger databases, you can schedule recalculations during off-peak hours. Before adding the command to a regular schedule, consider how frequently forecasts change and whether a full recalculation is necessary. Use filters when you only need to recalculate a specific group of forecasts.

Good to know

  • Errors may not stop the entire run. The described behavior is to report failures and continue processing other forecasts. Check the final error count and investigate any failures.
  • It recalculates existing records. The command is intended to update forecast rows that already exist. Do not assume it creates missing forecasts for new users or periods.
  • Saving uses forecast.save(). When a changed forecast is saved through this method, any applicable model behavior or signals may run.
  • Filters use IDs. The user, fiscal year, and forecast type options expect numeric IDs rather than names. You can find these IDs in the Django admin or the relevant record URL.
  • Processed counts include unchanged records. The total processed count represents the forecasts examined, not just the forecasts updated.

The recalculate_forecasts command helps bring stored forecast values back in line with current CRM data. It recalculates existing forecasts, reports differences, and can avoid unnecessary writes when values have not changed.

Start with –dry-run, review the proposed changes, and use the available filters to limit the operation when necessary. This makes forecast maintenance easier to review and safer to run on production databases.

Share this article