[Python] How can I trigger automatic actions when a model field (e.g. date) reaches a specific...

Discussão em 'Python' iniciado por Stack, Julho 30, 2025.

  1. Stack

    Stack Membro Participativo

    I’m working on a Django-based app where developers can connect users to their apps using an account number. Once connected, a Subscription model is created with fields like user, plan, is_active, and next_billing_date. The next_billing_date depends on the plan type (weekly, monthly, yearly, etc.).

    I want to automatically take some action (e.g., disable is_active or charge again) when the next_billing_date is reached. This includes:

    1. Deactivating expired subscriptions
    2. Or triggering recurring billing based on subscription cycle (e.g. Jan 1, 2025 → Jan 1, 2026 for yearly, or weekly for others)

    I already store the next billing date like this:

    next_billing_date = timezone.now().date() + timedelta(days=30) # For monthly plans


    I was trying to use signals to handle things like automatically charging users after 7 days or ending a subscription after 1 month. But I realized that Django signals only work when a model is saved, updated, or deleted, not based on time passing.

    This means if I want something to happen exactly 7 days after a user subscribes, a signal won’t help unless something else triggers it (like the model being saved again). So even if the subscription is about to expire, Django won’t do anything automatically unless some code updates that object.

    What is the best practice for implementing this kind of recurring billing logic in Django? Any suggestions, especially for time-based triggers in Django apps, would be appreciated.

    Continue reading...

Compartilhe esta Página