Django: Remove a ForeignKey while Retaining Customer Info for Ecommerce Orders

How can I enable members to delete their accounts while preserving key order details? I tried this idea using a new Purchase model:

from django.db import models
from django.contrib.auth import get_user_model

class Purchase(models.Model):
    buyer_account = models.ForeignKey(get_user_model(), on_delete=models.SET_NULL)
    order_email = models.EmailField()
    order_name = models.CharField(max_length=50)

    def sync_info(self):
        if not self.order_name:
            self.order_name = self.buyer_account.first_name

Hey Mia and everyone else, I’ve been tackling a similar challenge with preserving order details even after account deletions. One thought I had was to shift more of the customer’s persistent data into the Purchase model before deleting their user object. Your approach to sync fields like order_name seems effective, but I’m curious – have you considered triggering that sync automatically via signals? For example, a pre_delete signal on the user model could guarantee that all necessary details get transferred to the Purchase before the account goes away. Also, making sure that fields like buyer_account allow null values (i.e., using null=True and maybe even blank=True in your model) might save headaches later when an account is removed. I’d love to know if anyone’s had an experience with user soft deletion as an alternative approach in Django that preserves order history in a cleaner fashion. Does anyone opt for a self-referential ‘ghost’ user account that orphaned data points to, instead of outright nulling them? This could be interesting if you’re looking to maintain referential integrity without data loss. What does everyone think about these alternative strategies? Let’s discuss!

In my experience, it proves effective to decouple the user account from order details by transferring crucial customer data to the Purchase model before account deletion. A solid approach is creating an intermediary process where, at the moment of deletion, the relevant user information is copied to dedicated fields within Purchase. This method avoids data loss and maintains integrity, especially when combined with allowing the foreign key to be set to null. Relying on database level constraints alongside custom deletion signals can provide a robust and maintainable solution for preserving necessary historical data.

hey, maybe try a custom save hook to copy customer data on order creation so u dont depend only on signals. that way, even if deletion order glitches, the order info remains intact and its easier to spot any sync issues.