How to save many to many fields by using an auto complete text box
- by iHeartDucks
If I have two models like
class Author(models.Model):
name = models.CharField(max_length=100)
title = models.CharField(max_length=3, choices=TITLE_CHOICES)
birth_date = models.DateField(blank=True, null=True)
def __unicode__(self):
return self.name
class Book(models.Model):
name = models.CharField(max_length=100)
authors = models.ManyToManyField(Author)
I can render the book form using a model form like this
class BookForm(ModelForm):
class Meta:
model = Book
widgets = {
'authors' : TextInput()
}
The authors fields is now rendered as a text box and I want to use an auto complete (where I can enter multiple authors) text box to populate the field.
I am having a hard time to understand how I can save the authors in view function? I am thinking of using a hidden field to record all the author id's but I am having a hard time figuring out how to save it on the postback.