Home > Enterprise >  Django - Inline formset - How to set current user
Django - Inline formset - How to set current user

Time:11-27

-Objective- I need to set, in a Inline Formset, the current user as automatic content of a field of the form. (Currently not working)


Version Python 3.9.2 - Django 3.2

Context: I've created a List, where I have some objects(Headers). From the list I can create new headers and access to the detail-page(Detailview)for each of these by using a foreign-key(called by PK-primarykey).

In this page I can see more informations about 1 specific header. Each header can have multiple lines of informations that need to be linked to it. The lines are created or updated with (max 4 different) specific Inline Formsets.

-Issue and Error- I created the forms that are correctly rendered, but I need to set for each line, a field that automatically gets "current user" as its content. I can't save and receive instead"User cannot be null".

I'm unable to find a solutions and tried many things but stuck with this error.

Would kindly appreciate any help on how to solve this problem. Thanks in advance,


Below some code:

URLS.PY

from django.urls import path, re_path
from fttlapp import views

app_name= 'fttlapps'

urlpatterns = [
path('fttlapphome2/', views.Fttlapphome2View.as_view(), name='fttlapphome2'),
path('fttlogheader/', views.HeadfttlogListView.as_view(), name='headfttlogs'),
path('fttlogheader/add/', views.HeadfttlogCreateView.as_view(), name='headfttlogcreate'),
path('fttlogheader/<int:pk>/', views.HeadfttlogDetailView.as_view(), name='headfttlogdetail'),
path('fttlogheader/<int:pk>/flights/edit/', views.HeadfttlogDeafttlogEditView.as_view(), name='head_flight_edit'),
]

FORMS.PY

from django import forms
from django.contrib.auth.models import User
from django.db.models.fields import CharField, DateField
from django.forms import ModelForm, widgets
from django.forms.fields import ChoiceField
from django.forms.models import ModelChoiceField
from django.utils import timezone

# Load necessary to manage Form in Form
from django.forms.models import inlineformset_factory

# Load Tables
from fttlapp.models import Headfttlog, Deafttlog, Debfttlog
###############################################################################
# Forms for Headfttlog                                                        #
###############################################################################


class HeadfttlogcreateForm(ModelForm):
    class Meta:
        model = Headfttlog
        exclude = ['hea_creator', 'hea_modifier']

        widgets = {
            'hea_fttldate' : forms.TextInput(attrs={'type': 'date'}),
            'hea_nxtcheckdate' : forms.TextInput(attrs={'type': 'date'}),
            'hea_transfereddate' : forms.TextInput(attrs={'type': 'date'}),
        }
        ## Calendar widget to work both with Create and Update needs to be TextInput with type date
        ## Calendar widget not compatible with localized_fields !


###############################################################################
# Forms for Headfttlog with Deafttlog details view Management                 #
###############################################################################
HeadfttlogDeafttlogFormset = inlineformset_factory(Headfttlog, Deafttlog, 
    fields=('dea_linetype', 'dea_fttlcode', 'dea_airportfrom', 'dea_airportto',
            'dea_instrtimestart', 'dea_instrtimeend', 'dea_instrtimetot',
            'dea_blocktimestart', 'dea_blocktimeend', 'dea_blocktimetot',
            'dea_flighttimestart', 'dea_flighttimeend', 'dea_flighttimetot', 
            'dea_approach', 'dea_landing', 'dea_external', 'dea_fuel', 'dea_oil',
            'dea_lessonnotes', 'dea_preflightsignature', 'dea_invoiceaccount',
            'dea_transfered', 'dea_transfereddate',
            'dea_reccanceled', 'dea_creator', 'dea_modifier'),
    widgets={
            'dea_instrtimestart' : forms.TimeInput(format= '%H:%M', attrs={'type': 'time'}),
            'dea_instrtimeend' : forms.TimeInput(format= '%H:%M', attrs={'type': 'time'}),
            'dea_instrtimetot' : forms.TimeInput(format= '%H:%M', attrs={'type': 'time'}),
            'dea_blocktimestart' : forms.TimeInput(format= '%H:%M', attrs={'type': 'time'}),
            'dea_blocktimeend' : forms.TimeInput(format= '%H:%M', attrs={'type': 'time'}),
            'dea_blocktimetot' : forms.TimeInput(format= '%H:%M', attrs={'type': 'time'}),
            'dea_flighttimestart' : forms.TimeInput(format= '%H:%M', attrs={'type': 'time'}),
            'dea_flighttimeend' : forms.TimeInput(format= '%H:%M', attrs={'type': 'time'}),
            'dea_flighttimetot' : forms.TimeInput(format= '%H:%M', attrs={'type': 'time'}),
            'dea_reccanceled' : forms.HiddenInput,
            'dea_creator' : forms.HiddenInput,
            'dea_modifier' : forms.HiddenInput,
        }, extra=1, max_num=8)


VIEWS.PY

from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.urls import reverse
# Load necessary view
from django.views.generic import (
    TemplateView, ListView, CreateView, DetailView, FormView)
from django.views.generic.detail import SingleObjectMixin
# Load for security and access management
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.mixins import PermissionRequiredMixin
# Load functions and tools for search bar management
from fttlapp.filters import HeadfttlogFilter
import operator
from functools import partial, reduce
from django.db.models import Q
# Load functions for messages management
from django.contrib import messages
# Load Tables & Forms
from fttlapp.models import Headfttlog
from fttlapp.forms import HeadfttlogcreateForm
from fttlapp.forms import HeadfttlogDeafttlogFormset


###############################################################################
# FTTL home page management                                                   #
###############################################################################


@login_required
def fttlapphome(request):
    context = {}
    return render(request, 'fttlapp/fttlapphome.html', context)


###############################################################################
# FTTL home page 2 management                                                 #
###############################################################################

class Fttlapphome2View(TemplateView, LoginRequiredMixin):
    template_name = 'fttlapp/fttlapphome2.html'

###############################################################################
# Headfttlog - List view Management                                           #
###############################################################################


class HeadfttlogListView(ListView, LoginRequiredMixin, PermissionRequiredMixin):
    permission_required = 'fttlapp.view_headfttlog'

    model = Headfttlog
    template_name = 'fttlapp/headfttlog_list.html'
    paginate_by = 10

    def get_context_data(self, *args, **kwargs):
        context = super().get_context_data(*args, **kwargs)
        context['myFilter'] = HeadfttlogFilter(
            self.request.GET, queryset=self.get_queryset())
        return context

    # Following redefintion necessary to obtain correct pagination after Filter plugin
    def get_queryset(self):
        queryset = super().get_queryset()
        return HeadfttlogFilter(self.request.GET, queryset=queryset).qs


###############################################################################
# Headfttlog - Create view Management Headfttlog                              #
###############################################################################


class HeadfttlogCreateView(LoginRequiredMixin, PermissionRequiredMixin, CreateView):
    permission_required = 'fttlapp.add_headfttlog'

    model = Headfttlog
    template_name = 'fttlapp/headfttlogcreate_form.html'
    form_class = HeadfttlogcreateForm

    def form_valid(self, form):
        form.instance.hea_creator = self.request.user
        form.instance.hea_modifier = self.request.user
        messages.add_message(
            self.request,
            messages.SUCCESS,
            'The LOG has been created'
        )
        return super().form_valid(form)


###############################################################################
# Headfttlog - Detail view Management Headfttlog                              #
###############################################################################

class HeadfttlogDetailView(LoginRequiredMixin, PermissionRequiredMixin, DetailView):
    permission_required = 'fttlapp.view_headfttlog'

    model = Headfttlog
    template_name = 'fttlapp/headfttlogdetail.html'


###############################################################################
# Headfttlog with Deafttlog details view Management                             #
###############################################################################

class HeadfttlogDeafttlogEditView(LoginRequiredMixin, PermissionRequiredMixin, SingleObjectMixin, FormView):
    permission_required = ('fttlapp.add_headfttlog','fttlapp.change_headfttlog',
                             'fttlapp.add_deafttlog', 'fttlapp.change_deafttlog')

    model = Headfttlog
    template_name = 'fttlapp/head_flight_edit.html'

    ### 1. Identification of the single Headfttlog we will work with
    def get(self, request, *args, **kwargs):
        self.object = self.get_object(queryset=Headfttlog.objects.all())
        return super().get(request, *args, **kwargs)
    
    def post(self, request, *args, **kwargs):
        self.object = self.get_object(queryset=Headfttlog.objects.all())
        return super().post(request, *args, **kwargs)

    ### 2. FormSet creation - instance is the link to the above data. 
    def get_form(self, form_class=None):
        return HeadfttlogDeafttlogFormset(**self.get_form_kwargs(), instance=self.object)

    def formset_valid(self, form):
        form.dea_creator = self.request.user
        form.dea_modifier = self.request.user

        form.save()

        messages.add_message(
            self.request,
            messages.SUCCESS,
            'Changes were saved.'
        )

        return HttpResponseRedirect(self.get_success_url())
        
    def get_success_url(self):
        return reverse('fttlapps:headfttlogdetail', kwargs={'pk': self.object.pk})



###############################################################################
# End of view Management                                                      #
###############################################################################

HTML


{% extends 'base/dibase3.html' %}
{% load static %}

{% load crispy_forms_tags %}
{% crispy formset %}

{% block title %}Editing Flights and Instructions for {{ headfttlog.fttlcode }}{% endblock %}

{% block content %}


<style>

    .box{
        max-width: fit-content;
        margin: auto;

    }
</style>
 
</div>

<nav class="navbar navbar-expand-lg navbar-light bg-light">
    <div class="container-fluid">
      
        <div class="text-start">
          <a class="btn btn-outline-info " href="{% url 'fttlapps:headfttlogdetail' pk=headfttlog.pk %}">Back to Detail</a>
          
        </div>

     </div>
    </nav>
<hr>

<div class="container"> 

  <form action="" method="post" enctype="multipart/form-data">

    {% for hidden_field in form.hidden_fields %}
      {{ hidden_field.errors }}
      {{ hidden_field }}
    {% endfor %}

    {% csrf_token %}

    {{ form.management_form }}
    {{ form.non_form_errors }}

    <h3>Update Collection</h3>
    {% for deafttlog_form in form.forms %}
      <hr>
      <h5>
        {% if deafttlog_form.instance.id %}
          Deafttlog: {{ deafttlog_form.instance.dea_linetype }}
        {% else %}
          {% if form.forms|length > 1 %}
            Add another deafttlog
          {% else %}
            Add a deafttlog
          {% endif %}
        {% endif %}
      </h5>
      {% for hidden_field in deafttlog_form.hidden_fields %}
        {{ hidden_field.errors }}
      {% endfor %}
      <table>
        {{ deafttlog_form }}
      </table>
    {% endfor %}
    <hr>
    <p>
      <button type="submit" value="Update Flight" class="btn btn-primary w-100 mb-3">Update Flight</button>
      <a href="{{ author.get_absolute_url  }}" role="button" class="btn btn-secondary w-100">Return</a>
    </p>
  </form>

{% endblock content %}

CodePudding user response:

For audit fields like creator and modifier model fields I will usually set blank=True, null=True in the model field definition. Just by doing that your code will work, because you are already handling setting of dea_creator and dea_modifier in your views.

If you want to enforce this in the database like you are doing now, you will have to pass the request.user to your formset and set it as the initial value for the dea_creator field when initializing the forms.

Edit:

The alternative way I mentioned above, in your HeadfttlogDeafttlogFormset, instead of setting fields and widgets, create an actual form to use, for example DeafttlogForm, and set the fields and widgets there. Then in your formset, you set form=DeafttlogForm.

In the newly created DeafttlogForm, initialize the dea_creator field:

def __init__(self, *args, **kwargs):
    user = kwargs.pop('user', None)
    super(DeafttlogForm, self).__init__(*args, **kwargs)
    instance = getattr(self, 'instance', None)
    if not instance.id and user:
        self.initial['dea_creator'] = user

Then in HeadfttlogDeafttlogEditView, add a get_form_kwargs method:

def get_form_kwargs(self):
    kwargs = super(HeadfttlogDeafttlogEditView, self).get_form_kwargs()
    kwargs['user'] = self.request.user
    return kwargs
  • Related