How to use exclude to use in a form with django?

0

My doubt is to be able to make fields that are with exclude can be filled through a form but I still do not know how to show these same data

models.py code

 class Producto(models.Model):
     nombre = models.CharField(max_length=100)
     caracteristica= models.CharField(max_length=10)
     codigo = models.CharField(max_length=100)
     estado = models.BooleanField(default=True)
     observacion = models.CharField(max_length=100, default=True)

form.py code

 class ProductoForm(ModelForm):
     class Meta:
         model = Proveedor
         exclude = ['estado' , 'observacion']

views.py code

def crear_producto(request):
     if request.method == 'POST':
        form = ProductoForm(request.POST)
        if form.is_valid():
                form.save()
                return redirect('producto:producto_list')
 else:
     form = venta_form()
 return render(request, 'almacenes/productos_crear.html', {'form': form})

Button code where I call the form.html

  <a href="{% url 'producto:crear_producto' %}" class="btn btn-success" title="Baja Proveedor"> Crear </a>
   <!-- Parte del boton del codigo de los exclude -->
  <a href=" " class="btn btn-primary" title="Dar Obervacion y Estado">Mas ... </a>
    
asked by Moon lun 27.07.2018 в 05:20
source

1 answer

0

If I'm understanding you, what you're looking to do is a multi-page form. For this, exclude does not work for you, because this is done so that those fields are not taken into account in the form at any time.

If you want to make a multi-page form you can use form wizard from django-formtools , which is based on the creation of a form for each of the pages of the wizard.

Here's a simple example of how get it going.

If what you want is not a multi-page form, but rather two forms that do different things, simply create two forms. In this way:

class ProductCreateForm(forms.ModelForm):
    class Meta:
        model = Producto
        exclude = ['estado' , 'observacion']

class ProductUpdateStateForm(forms.ModelForm):
    class Meta:
        model = Producto
        fields = ['estado', 'observacion']

And you use each one when necessary.

    
answered by 01.08.2018 в 10:24