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 trialalejandro zaizar
16,452 PointsI 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.
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
Treehouse Moderator 68,441 PointsThe 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
.
Izen Huang
3,469 PointsIzen Huang
3,469 PointsHi Chris, can you explain as to why adding 'formset=ReviewForm' raises the error "not using the correct fields"
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsI 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().