Z
Z
zkweb2016-07-18 14:00:40
Django
zkweb, 2016-07-18 14:00:40

How to automatically indicate the author of the entry?

There is a model:

class Request(models.Model):
    **        
    usercreated = models.ForeignKey(User, blank=True, null=True)

How can I specify that the usercreated field be filled in the database automatically after (before) saving the record. The ID of the current user. I did it with the help of a signal, but it didn’t work out, because request cannot be found.
The project is only in the administrative interface, without cbv

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Z
zkweb, 2016-07-19
@zkweb

Here is the solution that helped me. Thanks to all!

def save_formset(self, request, form, formset, change):
        instances = formset.save(commit=False)
        for instance in instances:
            if isinstance(instance, Request):
                if(not instance.usercreated):
                    instance.usercreated = request.user          
                instance.save()

I
iegor, 2016-07-18
@iegor

You can catch the user in the middleware and then get him from there when saving

A
Alexander Lebedev, 2016-07-18
@zymanch

In my projects for a long time I did this:
1. Model with all common fields for audit:

models.py
class Audit(models.Model):
    """
    A common class for auditing the models' instances.
    """
    created_by = models.ForeignKey(
        User,
        related_name='%(app_label)s_%(class)s_created_related',
        verbose_name=_(u'Создал'),
        on_delete=models.SET_NULL,
        blank=True, null=True)
    changed_by = models.ForeignKey(
        User,
        related_name='%(app_label)s_%(class)s_changed_related',
        verbose_name=_(u'Изменил'),
        on_delete=models.SET_NULL,
        blank=True, null=True)
    created = models.DateTimeField(
        _(u'Дата создания'), auto_now_add=True)
    changed = models.DateTimeField(
        _(u'Дата изменения'), auto_now=True)
    class Meta:
        abstract = True

2. For admin panel:
admin.py
class AuditAdmin(admin.ModelAdmin):
    """
    Admin class for audit fields
    """
    readonly_fields = (
        'created_by',
        'changed_by',
        'created',
        'changed')

    fieldsets_preset = (
        _(u'Прочее'), {
            'fields': (
                'created_by',
                'created',
                'changed_by',
                'changed'
            )
        })
    actions_on_top = True

    def save_model(self, request, obj, form, change):
        if not change:
            obj.created_by = request.user
        obj.changed_by = request.user
        obj.save()

We inherit all models and admin classes for which it is necessary to monitor users, and rejoice.
This solution ceased to satisfy me as soon as there were too many models - the list of related relations for the User class became simply unbearable. I'm working on the transition to storing the user's login in text form instead of a foreign key per user.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question