A
A
Antigo_ptz2016-12-26 16:47:49
Django
Antigo_ptz, 2016-12-26 16:47:49

Django abstract model?

For all system models, I created an abstract model that stores the date of the last change of the record

class CommonInfo(models.Model):
    last_modified_date = models.DateTimeField(auto_now=True)

    class Meta:
        abstract = True

The question is that I need to add a field to this abstract model in which I need to store the username last_modified_user and every time any entity is saved, the user was substituted in this field. Does anyone have any ideas how this can be done?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
U
un1t, 2016-12-26
@Antigo_ptz

I have not tried to do this, but the general idea is this:
You can use middleware to write the current request to the thread variable (thread local). (For example https://github.com/nebstrebor/django-threadlocals)
Then it will be possible to get to the current user from the model.
In the abstract model, define a save method that will write the user attribute.

M
Mykola, 2016-12-26
@IKMOL

Alternatively, monitor the signals.

@receiver(post_save, sender=ModelName)
def funcname(sender, instance, **kwargs):
    #код обработки
    pass

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question