17

I am using inline formsets.

My model:

class Author(models.Model):
    description = models.CharField(max_length=100)

class Book(models.Model):
    author = models.ForeignKey(Author)
    details = models.CharField(max_length=100)

class AuthorForm(ModelForm):
    class Meta:
        widgets = {
            'description': Textarea(attrs={'cols': 40, 'rows': 4}),
        }

In my views.py I made the form from the AuthorForm like so

form = AuthorForm(request.POST)

But I also made a formset for the Books

InlineFormSet = inlineformset_factory(Author, Books)

I cannot pass in a BooksForm with widgets, so how do I add a textarea widget to the book details.

Is it even possible? Am I missing something obvious?

2 Answers 2

31

Try:

class BookForm(ModelForm):
    class Meta:
        model = Book
        widgets = {
            'details': Textarea(attrs={'cols': 40, 'rows': 4}),
        }


InlineFormSet = inlineformset_factory(Author, Book, form=BookForm)

Update by Wtower

This is great. Specifically for widgets, as of Django 1.6 there is a widgets parameter for inlineformset_factory

Sounds like you can now call

inlineformset_factory(Author, Book, widgets={'details': Textarea(attrs={'cols': 40}))
Sign up to request clarification or add additional context in comments.

2 Comments

Glad it actually worked! I couldn't easily find any docs either, I just looked at the source.
This is great. Specifically for widgets, as of Django 1.6 there is a widgets parameter for inlineformset_factory: stackoverflow.com/a/30192034/940098
0

I dont know if i have understand your question: do you want Book.details rendered as a textarea?

If so, just use a TextField instead a CharField.

class Author(models.Model):
   description = models.TextField()

1 Comment

this is correct for a textfield, but im looking for somkething that also does radio buttons, checkboxes etc. Was trying to keep my example simple. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.