DEV Community

Mirzonabot
Mirzonabot

Posted on

How to get current user in save method of a django model?

I have the following django model:

class Listing(models.Model)
    STATUS_CHOICES = [
        ('available', 'Available'),
        ('rented', 'Rented'),
        ('unavailable', 'Unavailable'),
        ('fake', 'Fake'),
    ]

    id = models.AutoField(primary_key=True)
    title = models.CharField(max_length=200)
    description = models.TextField()
    status = models.CharField(max_length=12, choices=STATUS_CHOICES, default='available')
    def save(self, *args, **kwargs):
        ## 
        if self.status == 'fake' and  ......:
            raise PermissionDenied("Cannot save listing with

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay