Warm tip: This article is reproduced from serverfault.com, please click

Django don't save a form using Floppyforms

发布于 2020-12-02 21:02:17

I have a field called medicamento in my model that is a foreign key and I want all the values in that field to be displayed in a searcheable dropdown in my form. I was able to do that but when I try to save, it says "Select a valid choice. That choice is not one of the available choices." Hope u can help dudes! Thanks in advance

models.py

class Stockmov(models.Model):
numero = models.AutoField(primary_key=True)
created= models.DateTimeField(auto_now_add=True,verbose_name="Fecha de Movimiento")
author = models.ForeignKey(User,verbose_name="autor",on_delete=models.PROTECT, null=True, blank=True)
medicamento= models.ForeignKey(Medicamento,on_delete=models.CASCADE)
motivo= models.CharField(max_length=200)
Cantidad = models.IntegerField()

forms.py

class StockmovForm(forms.ModelForm):
class Meta:
    model = Stockmov

    fields =  ['medicamento', 'motivo', 'Cantidad' ]
    widgets = {
    #'medicamento': forms.Select(attrs={'class':'form-control', 'placeholder':'Medicamento'}),
    'medicamento': forms.widgets.TextInput(attrs={'class':'form-control', 'placeholder':'Medicamento'},datalist=Medicamento.objects.all()), 
    'motivo': forms.TextInput(attrs={'class':'form-control', 'placeholder':'Motivo'}),
    'Cantidad': forms.NumberInput(attrs={'class':'form-control', 'placeholder':'Cantidad'}),
    }
    labels = {
        'medicamento':'Medicamento', 'motivo':'Motivo del Movimiento', 'Cantidad':'Cantidad del Movimiento',
    }

views.py

class StockmovCreate(CreateView):
model = Stockmov
form_class = StockmovForm
success_url = reverse_lazy('stockmov:stockmov')

def form_valid(self, form):
    form.instance.author = self.request.user
    print(self.request.user)
    return super(StockmovCreate, self).form_valid(form)

Template

      <form action="" method="post">{% csrf_token %}
       {{ form.as_p }}`s`
        <div class="text-center">
          <input type="submit" id="btnCrear" class="btn btn-secondary btn-block" value="Crear Movimiento" />
        </div>
      </form>
Questioner
wwrandazzo
Viewed
0
wwrandazzo 2020-12-05 06:26:31

Workarround: I removed floppyforms on my Project and use Select2 instead because it supports searches on foreignkey field and also create an instance with a form easily.

Template

<script>
$(document).ready(function() {
  $('#id_medicamento').select2();
});
</script>

forms.py

class StockmovForm(forms.ModelForm):
class Meta:
model = Stockmov

fields =  ['medicamento', 'motivo', 'Cantidad' ]
widgets = {
'medicamento': forms.Select(attrs={'class':'form-control', 'placeholder':'Medicamento'}),
'motivo': forms.TextInput(attrs={'class':'form-control', 'placeholder':'Motivo'}),
'Cantidad': forms.NumberInput(attrs={'class':'form-control', 'placeholder':'Cantidad'}),
}
labels = {
    'medicamento':'Medicamento', 'motivo':'Motivo del Movimiento', 'Cantidad':'Cantidad del Movimiento',
}