I have a problem that I am using 2 libraries: django-**autocomplete-light and django-dynamic-formset . They are both very good at doing their jobs. I use the first one for autocompletion in a foreign key field and the second one to make django formsets dynamic, that is, to be able to add or remove formsets. but when you want to join these 2 a problem occurs.
Here it works correctly because it is only 1
It stops working and I'm trying to fix that problem as it seems to be the way the django-dynamic-formset library duplicates formsets
My Template:
{% extends 'base/base.html' %}
{% load static %}
{% block titulo%} Registrar venta {%endblock%}
{% block contenido %}
<div class="col-md-12">
<form method="post">{% csrf_token %}
<div class="col-md-4 form-group">
<label class="font-weight-bold" for="{{form.cliente.name}}">
{{form.cliente.label}}</label>
{{form.cliente}}
</div>
<h4 class="text-left">Detalle de venta: </h4>
<div class="table-responsive-sm">
<table class="table" id="tablaDetalle">
{{ detalleformset.management_form }}
<thead class="thead-dark">
<th>Producto</th>
<th width="100px">Cantidad</th>
<th width="115px">Prec.Unit.</th>
<th width="115px">Subtotal</th>
<th>Acción</th>
</thead>
<tbody>
{% for form in detalleformset.forms %}
<tr class="formset_row">
{% for field in form.visible_fields %}
<td>
{# Include the hidden fields in the form #}
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{{ field.errors.as_ul }}
{{ field }}
</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div class="row justify-content-md-end">
<div class="col-md-2">
<label class="font-weight-bold" for="{{form.total.name}}">{{form.total.label}}</label>
{{form.total}}
</div>
</div>
<div class="form-group">
<label class="font-weight-bold" for="{{form.descripcion.name}}">{{form.descripcion.label}}</label>
{{form.descripcion}}
</div>
<div class="col-md-4 offset-md-4">
<button class="btn btn-block btn-lg btn-primary" type="submit"><span><i class="fa fa-shopping-cart"></i>
</span>Registrar venta</button>
</div>
</form>
</div>
{% endblock %}
{% block javascript %}
<script type="text/javascript" src="{% static 'admin/js/vendor/jquery/jquery.js' %}"></script>
{{ detalleformset.media }}
<script src="{% static 'js/jquery.formset.js' %}"></script>
<script type="text/javascript">
$('.formset_row').formset({
addText: 'Agregar Producto',
deleteText: 'remover',
prefix: 'detalleventa'
});
</script>
{% endblock %}
Is there any solution to this? I've been looking but there is little information.
UPDATE forms.py:
from dal import autocomplete
from django import forms
from apps.venta.models import Venta,DetalleVenta
from django.forms import inlineformset_factory
class VentaForm(forms.ModelForm):
class Meta:
model = Venta
fields = [
'cliente',
'descripcion',
'total',
]
labels = {
'cliente':'Cliente:',
'descripcion':'Descripción de pedido:',
'total':'Total S/.',
}
widgets ={
'cliente':forms.TextInput(attrs={'placeholder':'Ejm:Nicolas','class':'form-control','required':True}),
'descripcion': forms.Textarea(attrs={'rows':'3','placeholder':'Ejm: 1 con ensalada y la otra solo mostaza'
,'class': 'form-control'}),
'total': forms.NumberInput(attrs={'class':'form-control-plaintext total','placeholder': '0.00','required':True, 'readonly':True}),
}
class DetalleForm(forms.ModelForm):
class Meta:
model = DetalleVenta
fields = [
'producto',
'cantidad',
'preciounit',
'subtotal',
]
labels = {
'producto':'Producto',
'cantidad':'Cantidad',
'preciounit':'Prec.Unit.',
'subtotal':'Subtotal',
}
widgets = {
'producto': autocomplete.ModelSelect2(url='producto-autocomplete'),
'cantidad':forms.NumberInput(attrs={'class':'form-control cantidad', 'required': True}),
'preciounit':forms.NumberInput(attrs={'class':'form-control-plaintext', 'placeholder':'0.00','readonly':True}),
'subtotal':forms.NumberInput(attrs={'class':'form-control-plaintext subtotal', 'placeholder':'0.00','readonly':True}),
}
DetalleFormSet = inlineformset_factory(Venta, DetalleVenta,
form=DetalleForm, extra=1)
Last year I managed to solve it and sorry if I didn't publish the answer since I haven't had much time due to work
The django-autocomplete-light library works fine but when you want to create or add dynamic forms which it was in my case, it fails.
What's going on? adding a new form with the django-dynamic-formset library effectively added a new field but django-autocomplete-light doesn't reload its css.
Luckily, this django-autocomple-light library uses select2 and well, it is simply called every time we click on a new one, load the css of it:
First let's create the function that calls the css of select2
and then when we click on add a new one we call this function in the added and to the last one I also added that it is executed when the page loads to put the same placeholder but it is optional:
and well that's the solution :D anyway I recommend you to stop using jquery and switch to vuejs , reactjs or angularjs and django only as backend that for this we have django-rest-framework :D! A hug!!