2

I asked this question earlier, but now I'm having trouble sorting out how to use drop downs (or even better, autofill fields) for one of the forms of a multi-form view.

The models in play are Book, BookDetails, and Genre. BookDetails is a linking table to Genre (and other similar tables) so that I can have a static list of genres etc. with unique IDs and foreign keys to BookDetails.

Right now I have this:

#views.py
def BookFormView(request):
        genre  = Genre.objects.all()
        if request.method == "POST":
            book_form = BookForm(request.POST, prefix='book')
            bookdetails_form = BookDetailsForm(request.POST, prefix='bookdetails')
            selected_genre = get_object_or_404(Genre, pk=request.POST.get('genre_id'))
            genre.id = selected_genre
            genre.save()
            if book_form.is_valid() and bookdetails_form.is_valid():
                book_form.save()                    
                bookdetails_form.save()
                return HttpResponseRedirect("/books/")  
        else:
            book_form = bookForm(prefix='book')
            bookdetails_form = BookDetailsForm(prefix='bookdetails) 
        return render(request,  'books/createbook.html',
            {'book_form' : book_form, 
             'bookdetails_form': bookdetails_form,
             'genre':genre,})

#createbook.html
<select name="genre", id="genre" form="bookform">
        {% for entry in genre %}
            <option value="{{ entry.id }}">
                {{ entry.name }}
            </option>
        {% endfor %}
 </select>

The form displays properly on the page, dropdown menu with options from the database included. However, when I hit submit to store the information to the database I get an error saying No Genre matches the given query The other posts on SO that regard this error don't seem to be from the same context. I think that it might be something to do with selecting a name but storing an id (for the genres), but otherwise I'm at a loss.

Community
  • 1
  • 1
Nancy
  • 3,989
  • 5
  • 31
  • 49

1 Answers1

1

Normally, the way you'd do this with a form in django is not by manually pulling something out of the POST dict, but by using a ModelChoiceField:

https://docs.djangoproject.com/en/1.8/ref/forms/fields/#modelchoicefield

Was there a specific reason you didn't do that?

Also, it appears you're using the genre variable incorrectly for two different things. You initialize it with a queryset, but then try to treat it like a Genre instance later in the code. That's going to cause problems not to mention the fact that I don't think your genre.id = ... line is going to do what you expect it to.

Also, it's against style conventions to use title-casing for function names. If you're going to be doing much coding in Python, it's probably worth taking a look at the officially accepted PEP8 style guide here:

https://www.python.org/dev/peps/pep-0008/

There are a few other problems in the code but I'm not sure it's worth calling them out.

David Sanders
  • 4,069
  • 1
  • 24
  • 38
  • 1
    Great feedback, thanks. It looks like I definitely should use a ModelChoiceFieldl I'll take a look at the style guide and report back with the right vocabulary to say what I was attempting to do. – Nancy May 20 '15 at 19:40
  • @Nancy My only point with the style guide was to say that `def BookFormView...` should be `def book_form_view...`. Also, if you feel like my answer solved your problem you should click the checkbox next to it to accept it and/or click the up-arrow to up-vote it. – David Sanders May 20 '15 at 19:45
  • It was certainly helpful, but my code is still the same. I'm using the resources you mentioned, but I can't yet tell if my problem is solved. – Nancy May 20 '15 at 20:07