Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Django Forms Inlines and Media Create an Inline Model Formset

alejandro zaizar
alejandro zaizar
16,452 Points

I think something is broken in this code challenge, django forms create inline model formset quiz part 1

So this is the first part of this code challenge: I want to be able to create a Review in the same form as where I create a Product. That means I need an inline form! Create an inline model formset factory, named ReviewFormset, for the Review model. You need to include all the same fields as the existing ReviewForm. Remember, the first argument to the factory is the parent model (Product) and the second is the model the factory is for (Review).

The error says that i'm not using the correct fields, but i have copy and paste the fields from the default code. Can somebody tell what is wrong in here.

articles/forms.py
from django import forms

from . import models
from products.models import Product


class ReviewForm(forms.ModelForm):
    class Meta:
        model = models.Review
        fields = ('headline', 'rating', 'content', 'writer', 'publish_date')

ReviewFormset = forms.inlineformset_factory(
    models.Product,
    models.Review,
    fields = ('headline', 'rating', 'content', 'writer', 'publish_date'),
    formset = ReviewForm
)

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

The formset = ReviewForm line is causing the problem and should be removed:

ReviewFormset = forms.inlineformset_factory(
    models.Product,
    models.Review,
    fields = ('headline', 'rating', 'content', 'writer', 'publish_date'),
    # formset = ReviewForm
)

Also, since Product is explicitly imported, models.Product can be optionally replaced with Product.

Hi Chris, can you explain as to why adding 'formset=ReviewForm' raises the error "not using the correct fields"

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

I can’t be sure of the challenge checker error messages. Looking at the modelforeset_factory docs, the argument formset is passed through to formset_factory(), instead of the desired modelform_factory().