Solucionado (ver solução)
Solucionado
(ver solução)
1
resposta

Form não cria o objeto no Banco de Dados

Estou tentando fazer com que um form crie o objeto no banco, a função retorna mensagem de sucesso... mas o objeto não é criado.

Função para registrarno banco

@login_required
def register_company(request):
    form = NewCompany()

    if request.method == 'POST':
        form = NewCompany(request.POST)
        if form.is_valid():
            name_company = form.cleaned_data['name_company']
            cnpj_company = form.cleaned_data['cnpj_company']
            area_company = form.cleaned_data['area_company']
            manager_company = form.cleaned_data['manager_company']
            email_company = form.cleaned_data['email_company']
            status = form.cleaned_data['status']
            enterprise = form.cleaned_data['enterprise']
            logo_company = form.cleaned_data['logo_company']

            form = Company(name_company=name_company, cnpj_company=cnpj_company, area_company=area_company,
                           manager_company=manager_company, email_company=email_company, status=status,
                           enterprise=enterprise, logo_company=logo_company)

            form.save()

        messages.success(request, 'Empresa salva com sucesso!')

        return HttpResponseRedirect('list_company')

    else: 
        enterprise = {'enterprise': Enterprise.objects.all()}

        return render(request, 'company/register_company.html', {'enterprise': enterprise, 'form': form})

Form HTML

<div class="container">
    <form action="{% url 'register_company' %}" method="post" enctype="multipart/form-data">

        {% csrf_token %}

        <input type="text" id="tool_id" class="form-control" name="company_id" hidden>

       <div class="row">
           <div class="col-md-6">
               <div class="form-group">
            <label><b>Nome da Empresa</b></label>
            <input type="text" class="form-control" name="name_company" required>
               </div>
           </div>
           <div class="col-md-6">
           <label><b>CNPJ</b></label>
               <div class="form-group">
                   <input type="text" class="form-control"  name="cnpj_company" required>
               </div>
           </div>
       </div>

       <div class="row">
            <div class="col-md-4">
               <div class="form-group">
               <label><b>Área Responsável</b></label>
                   <input type="text" class="form-control"  name="area_company" required>
               </div>
           </div>
           <div class="col-md-4">
               <div class="form-group">
               <label><b>Responsável da Empresa</b></label>
                   <input type="text" class="form-control" name="manager_company" required>
               </div>
           </div>
           <div class="col-md-4">
               <label><b>Unidade</b></label>
                   <div class="form-group">
                    <select id="enterprise_id" class="form-control form-control-md"  name="enterprise_id" required>
                            <option selected>Selecione a Unidade</option>
                         {% for enterprise in enterprise.enterprise %}
                            <option value="{{enterprise.id}}">{{enterprise.enterprise_name}}</option>
                        {% endfor %}
                     </select>
                   </div>
           </div>
       </div>

...

       <div class="row">
           <div class="col-md-6">
            <label><b></b></label>
                <button class="btn btn-icon btn-3 btn-primary" type="submit" value="register_company">
                    <span class="btn-inner--icon"><i class="ni ni-folder-17"></i></span>
                    <span class="btn-inner--text">Criar Empresa</span>
                </button>
            </div>
       </div>
    </form>
</div>

{% endif %}

Form que está no arquivo forms.py

from django.forms import ModelForm
from .models import Company


class NewCompany(ModelForm):
    class Meta:
        model = Company
        fields = ['name_company', 'cnpj_company', 'area_company', 'manager_company', 'email_company','status',
                 'logo_company', 'enterprise']
1 resposta
solução!

Resolvido.