Preserving customer data in Django when deleting user accounts for e-commerce

I’m working on an e-commerce project using Django and I’m stuck on how to handle user data when they delete their account. Here’s my situation:

I have two models: Customer and Purchase. I want to keep the customer’s info for each purchase even if they delete their account. But I’m not sure how to do this without duplicating data.

Right now, my Purchase model looks something like this:

class Purchase(models.Model):
    buyer_email = models.EmailField()
    buyer_name = models.CharField(max_length=100)
    buyer = models.ForeignKey(Customer, on_delete=models.SET_NULL, null=True)

    def prepare(self):
        if not self.buyer_name:
            self.buyer_name = self.buyer.name
        if not self.buyer_email:
            self.buyer_email = self.buyer.email

    def save(self, *args, **kwargs):
        self.prepare()
        super().save(*args, **kwargs)

This feels wrong. Is there a better way to keep customer data without copying fields from the Customer model? Or is this duplication unavoidable? Any advice would be great!

Hey there, Liam_Stardust! Your question got me thinking about some interesting approaches. Have you considered using a soft delete mechanism? It’s pretty nifty!

Instead of copying data, why not add an ‘is_active’ field to your Customer model? When someone wants to delete their account, you just flip that to False. That way, you keep all the info without messing up your database relationships.

But here’s a thought - what about privacy concerns? How do you balance keeping purchase records with respecting user requests to be ‘forgotten’? Maybe you could anonymize the data after a certain period?

Oh, and have you looked into Django’s built-in stuff for this kind of thing? I heard there might be some cool features or packages that could help.

What do you think about these ideas? Have you tried anything like this before? I’d love to hear more about your project and what you end up deciding!

I’ve dealt with a similar issue in my e-commerce projects. Instead of duplicating data, consider implementing a soft delete mechanism. Add an ‘is_active’ boolean field to your Customer model and set it to False when a user deletes their account. This way, you preserve the data without violating referential integrity.

You could modify your Purchase model to use a custom manager that filters out inactive customers by default. This approach maintains data consistency and allows you to keep purchase records intact without unnecessary duplication.

Remember to update your views and queries to respect the ‘is_active’ status. Also, ensure your data retention policies comply with relevant privacy regulations. This method has worked well for me in maintaining historical data while respecting user deletion requests.

hey Liam, i’ve deaLt with this before. instead of copying data, why not use a soft delete? add an ‘is_active’ field to ur Customer model and set it to False when someone deletes their account. This way u keep the data without breaking relationships.

maybe look into django packages for this too? they might have some cool solutions. what do u think about this approach?