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

WBOY
Release: 2024-08-12 18:45:13
Original
678 people have browsed it
How to get current user in save method of a django model? How to get current user in save method of a django model?
How to get current user in save method of a django model?
0
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
Copy after login
Open Full Question

The above is the detailed content of How to get current user in save method of a django model?. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template