Hey i managed to make a inlineformset_factory but my widget in the Parent Model are not working although i have specified them in the ModelForm
.
My forms.py :
class PostForm(forms.ModelForm):
post = forms.CharField(widget=CKEditorWidget())
class Meta:
model = Post
fields = ['title', 'author','picture','post','draft','publish']
class PostVocabForm(forms.ModelForm):
class Meta:
model = PostVocab
exclude = ()
PostVocabInlineFormSet = inlineformset_factory(
Post,
PostVocab,
extra=1,
exclude=(),
)
My CKEditorWidget is not working ....
My views.py:
class PostPostVocabCreate(CreateView):
model = Post
form_class = PostForm
# fields = ['title', 'author', 'picture', 'post', 'draft', 'publish']
def get_redirect_url(self, pk):
return reverse_lazy('blog:post_detail',
kwargs={'slug': pk},
)
def get_context_data(self, **kwargs):
data = super(PostPostVocabCreate, self).get_context_data(**kwargs)
if self.request.POST:
data['postvocabs'] = PostVocabInlineFormSet(self.request.POST)
else:
data['postvocabs'] = PostVocabInlineFormSet()
return data
def form_valid(self, form):
context = self.get_context_data()
postvocabs = context['postvocabs']
with transaction.atomic():
self.object = form.save()
if postvocabs.is_valid():
postvocabs.instance = self.object
postvocabs.save()
return super(PostPostVocabCreate, self).form_valid(form)
I guess that my widget from the Parent model (Post) was overwritten while using a inlineformset_factory...
You can set widgets inside of inlineformset_factory.
PostVocabInlineFormSet = inlineformset_factory(
Post,
PostVocab,
extra=1,
exclude=(),
widgets={'post': CKEditorWidget()
)
From Django docs...inlineformset_factory uses modelformset_factory and passes most of its arguments to modelformset_factory. This means you can use the widgets parameter in much the same way as passing it to modelformset_factory.
AuthorFormSet = modelformset_factory(
... Author, fields=('name', 'title'),
... widgets={'name': Textarea(attrs={'cols': 80, 'rows': 20})})`
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.