Skip to content Skip to sidebar Skip to footer

Modelchoicefield Invalid Literal For Int() With Base 10: ''

So I've spent the day trying to chase down a custom thing that I wanted to achieve using FormView. When I use FormView with HTML form method='POST', I am able to get the desired r

Solution 1:

defget_object(self, queryset=None):
    foo = get_object_or_404(Author, id=self.request.GET.get("dropdown"))
    return foo

Um, I'm a little baffled myself, but this doesn't look right, the two return statements. Try something like above. Also:

  self.fields['dropdown'].empty_label = None

Solution 2:

Leveraging this SO....How to prevent submitting the HTML form's input field value if it empty I was able to determine that the best way to go about this was to add the following Javascript to by my code...

$('form').submit(function() {
  var dropdown = $('#id_dropdown').val();
  if (dropdown === undefined || dropdown === "") {
    $('#id_dropdown').attr('name', '' );
  } else {
    $('#id_dropdown').attr('name', 'dropdown');
  }
});
  });

The code above forces a 404 instead of the invalid literal message that I was getting before.

Post a Comment for "Modelchoicefield Invalid Literal For Int() With Base 10: ''"