How to save many to many fields by using an auto complete text box

Posted by iHeartDucks on Stack Overflow See other posts from Stack Overflow or by iHeartDucks
Published on 2010-06-14T00:25:18Z Indexed on 2010/06/14 0:32 UTC
Read the original article Hit count: 486

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about django

Related posts about django-forms