Hey thanks a ton for this tutorial, im still quite new to django so I was wondering how would I go about adding a validation so at least one CollectionTitle is required for each Collection, thanks alot!
So I've figured out how to add validation for the CollectionTitle, but im doing it through views.py. Wondering if anybody knows how to add the validation to client side instead of going through views, thanks.
How I am doing it at the moment is adding a min_num and validate_min to the inlineformset_factory.
CollectionTitleFormSet = inlineformset_factory(
Collection, CollectionTitle, form=CollectionTitleForm,
fields=['name', 'language'], can_delete=True, min_num=1, validate_min=True, extra=0
And by setting an else function in the form_valid. I save the object after validating titles since my project requires at least one title for each collection.
#kept rest the same till form_valid
def form_valid(self, form):
context = self.get_context_data()
titles = context['titles']
form.instance.created_by = self.request.user
self.object = form.save()
titles.instance = self.object
return super(CollectionCreate, self).form_valid(form)
We're a place where coders share, stay up-to-date and grow their careers.
We strive for transparency and don't collect excess data.