S
S
Stanislav Fateev2015-08-06 16:03:25
Django
Stanislav Fateev, 2015-08-06 16:03:25

Django. How to get the current user in the model method? And is it right?

I am writing an event system. Guided by the principle of fat models, I want to put the functionality of creating an event into the save method of the model. So that when the object changes, the corresponding event is created. There was a gag with the user identification - it is necessary that it be saved in the event when the save () call comes from the form, and that nothing would be saved if save () was called by something else (not by the user). Can you tell me the django way for this case?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Kitaev, 2015-08-06
@deliro

In the view:
In the model:

def save(self, *args, **kwargs):
    user = kwargs.pop('user')
    ...
    super().save(*args, **kwargs)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question