Reputation: 5890
Using the following i can get a multi select area, but what i would like is to have users be able to add authors using <input type="text">
for author name and a drop down for title. The way it is written we would have to know all the possible authors for books which is not plausible. Ideally there would be + so someone could add multiple authors to the same book.
I am using form based views, you can see the code as of the writing of this question here
from django.db import models
from django import forms
TITLE_CHOICES = (('MR', 'Mr.'),('MRS', 'Mrs.'),('MS', 'Ms.'))
class Author(models.Model):
name = models.CharField(max_length=80)
title = models.CharField(max_length=3, choices=TITLE_CHOICES)
def __unicode__(self):
return self.name
class Book(models.Model):
name = models.CharField(max_length=200)
authors = models.ManyToManyField(Author)
def __unicode__(self):
return self.name
class AuthorForm(forms.ModelForm):
class Meta:
model = Author
fields = '__all__'
widgets = {
'name': forms.TextInput(attrs={'cols': 80, 'rows': 20}),
'title': forms.TextInput(attrs={'cols': 80, 'rows': 20})
}
class BookForm(forms.ModelForm):
class Meta:
model = Book
fields = '__all__'
def save(self, request, commit=True):
super(BookForm, self).save(request)
pass
The html might be something like this (with different name
attributes)
<input type="text" name="booktitle">
<div>
<select><option>Mr</option><option>Mrs</option></select>
<input type="text" name="authorName">
<button name="clickOneToAddAnotherAuthor">
</div>
<div>
<select><option>Mr</option><option>Mrs</option></select>
<input type="text" name="authorName">
<button name="clickOneToAddAnotherAuthor">
</div>
Upvotes: 1
Views: 634
Reputation: 566
You could use a ChoiceField
class AuthorForm(forms.ModelForm):
title = forms.ChoiceField()
class Meta:
model = Author
fields = '__all__'
widgets = {
'name': forms.TextInput(attrs={'cols': 80, 'rows': 20}),
'title': forms.Select(attrs={'cols': 80, 'rows': 20})
}
Upvotes: 1