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

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

Aug 11 ’24
Comments: 1
Answers: 0


This content originally appeared on DEV Community and was authored by Mirzonabot

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


This content originally appeared on DEV Community and was authored by Mirzonabot


Print Share Comment Cite Upload Translate Updates
APA

Mirzonabot | Sciencx (2024-08-11T16:17:07+00:00) How to get current user in save method of a django model?. Retrieved from https://www.scien.cx/2024/08/11/how-to-get-current-user-in-save-method-of-a-django-model/

MLA
" » How to get current user in save method of a django model?." Mirzonabot | Sciencx - Sunday August 11, 2024, https://www.scien.cx/2024/08/11/how-to-get-current-user-in-save-method-of-a-django-model/
HARVARD
Mirzonabot | Sciencx Sunday August 11, 2024 » How to get current user in save method of a django model?., viewed ,<https://www.scien.cx/2024/08/11/how-to-get-current-user-in-save-method-of-a-django-model/>
VANCOUVER
Mirzonabot | Sciencx - » How to get current user in save method of a django model?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/11/how-to-get-current-user-in-save-method-of-a-django-model/
CHICAGO
" » How to get current user in save method of a django model?." Mirzonabot | Sciencx - Accessed . https://www.scien.cx/2024/08/11/how-to-get-current-user-in-save-method-of-a-django-model/
IEEE
" » How to get current user in save method of a django model?." Mirzonabot | Sciencx [Online]. Available: https://www.scien.cx/2024/08/11/how-to-get-current-user-in-save-method-of-a-django-model/. [Accessed: ]
rf:citation
» How to get current user in save method of a django model? | Mirzonabot | Sciencx | https://www.scien.cx/2024/08/11/how-to-get-current-user-in-save-method-of-a-django-model/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.