get the logged in user and save the id using inline formset

0

Hi, I'm having a problem getting the logged-in user ID simplifying, I have a form where the user will enter the information and when he gives the submit the user will also be saved together, because I will need the user because the system will distribute this form by several sectors

My .models

class Solicitacao(models.Model):
dt_emissao = models.DateTimeField(auto_now_add=True)
dt_entrega = models.DateTimeField(blank=True, null=True)
local_entrega = models.CharField(max_length=80)
motivacao = models.TextField()
solicitante = models.ForeignKey(Solicitante)
setor_nome = models.ForeignKey(OrgaoSetor)
autorizacao = models.ForeignKey(Autorizacao)
usuarios = models.ForeignKey(User, blank=True, null=False)


def __str__(self):
    return self.motivacao

class Meta:
    verbose_name = 'Solicitacao'
    verbose_name_plural = 'Solicitacoes'

class Servico(models.Model):
id_solicitacao = models.ForeignKey(Solicitacao)
qtde = models.CharField(max_length=10, blank=True, null=True)
unidade = models.CharField(max_length=10, blank=True, null=True)
c_Custo = models.CharField(max_length=10, blank=True, null=True)
descricao = models.CharField(max_length=100000, blank=True, null=True)

def __str__(self):
    return self.descricao

class Meta:
    verbose_name = 'Servico'
    verbose_name_plural = 'Servicos'

My .forms

class SolicitacaoForm(forms.ModelForm):

class Meta:
    model = Solicitacao
    fields = ['setor_nome',
    'solicitante',
    'motivacao',
    'dt_entrega',
    'local_entrega',
    'autorizacao',
    'usuarios']

class ServicoForm(forms.ModelForm): 

class Meta:
    model = Servico
    exclude = ()  

ServicoFormSet = inlineformset_factory(Solicitacao, Servico,fields('qtde', 'unidade', 'c_Custo', 'descricao'),extra=1)

and my views

class SolicitacaoServicoCreate(CreateView):
model = Solicitacao
template_name = 'Servico_create.html'
fields = ['setor_nome', 'solicitante', 'motivacao', 'dt_entrega', 'local_entrega', 'autorizacao', 'usuarios']
success_url = reverse_lazy ('home')

def get_context_data(self, **kwargs):
    data = super(SolicitacaoServicoCreate, self).get_context_data(**kwargs)
    if self.request.POST:
        data['servico'] = ServicoFormSet(self.request.POST)
    else:
        data['servico'] = ServicoFormSet()
    return data 

def form_valid(self, form):
    context = self.get_context_data()
    servico = context['servico']
    form.instance.servico = self.request.user
    with transaction.atomic():
        if form.is_valid():
            form.usuarios = self.request.user     
            self.object = form.save()
        if servico.is_valid():
            servico.instance = self.object
            servico.save()
    return super(SolicitacaoServicoCreate, self).form_valid(form)
    
asked by anonymous 22.11.2017 / 13:01

1 answer

0

solution is to pass the object

self.object = form.save(commit=False)
            self.object.usuarios = self.request.user
            self.object.save()
    
22.11.2017 / 13:29