Django, ValidationError in BaseModelFormSetWhat is a “slug” in Django?How do I do a not equal in Django queryset filtering?Does Django scale?What's the difference between django OneToOneField and ForeignKey?Saving form data rewrites the same rowDjango low level cache viewsdifferentiate null=True, blank=True in djangoDjango modelformset order_by not workingAccessing context variables in modelformunit testing django ModelFormset clean methods

Why did the metro bus stop at each railway crossing, despite no warning indicating a train was coming?

Why did the soldiers of the North disobey Jon?

How to check if comma list is empty?

c++ conditional uni-directional iterator

Can my American children re-enter the USA by International flight with a passport card? Being that their passport book has expired

Could there be something like aerobatic smoke trails in the vacuum of space?

Geometric inspiration behind Hal(irutan)'s Wolf(ram Language Logo)

Was the dragon prowess intentionally downplayed in S08E04?

Can anyone give me examples of the relative-determinative 'which'?

Slice a list based on an index and items behind it in python

Given 0s on Assignments with suspected and dismissed cheating?

Will consteval functions allow template parameters dependent on function arguments?

Why is the marginal distribution/marginal probability described as "marginal"?

Polynomial division: Is this trick obvious?

Does the Rogue's Reliable Talent feature work for thieves' tools, since the rogue is proficient in them?

Variance and covariance inequality

What dog breeds survive the apocalypse for generations?

Why did Varys remove his rings?

What is this weird d12 for?

Is there an academic word that means "to split hairs over"?

Why do galaxies collide?

When did game consoles begin including FPUs?

Can I say: "When was your train leaving?" if the train leaves in the future?

tikz drawing rectangle discretized with triangle lattices and its centroids



Django, ValidationError in BaseModelFormSet


What is a “slug” in Django?How do I do a not equal in Django queryset filtering?Does Django scale?What's the difference between django OneToOneField and ForeignKey?Saving form data rewrites the same rowDjango low level cache viewsdifferentiate null=True, blank=True in djangoDjango modelformset order_by not workingAccessing context variables in modelformunit testing django ModelFormset clean methods






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I am using BaseModelFormSet and passed it to modelformset_factory in views.And inside BaseModelFormSet function i override clean() to make formset validation to rise validation error.The problem is that when I try to fill the formset with a wrong data, validation error is not showing at all



I have tried to render from in template manually with non field errors but did not work and went through documentation https://docs.djangoproject.com/en/2.1/topics/forms/modelforms/#overriding-clean-on-a-modelformset



UPDATE: if I intentionally filled the form with wrong data , it will validate only the first form in formset not all the fomrs (extras set to 2)



forms.py



class MyModelFormSet(BaseModelFormSet):
def clean(self):
super().clean()
if any(self.errors)
return

for form in self.forms:
debit = form.cleaned_data['debit']
if debit == 100:
raise forms.ValidationError('debit is 100!')
return debit


views.py



def postpage(request):
journalLineFormSet = modelformset_factory(journalLine,exclude=('journalNumber',),extra=2,can_delete=True,formset=MyModelFormSet,max_num=2,validate_max=True)
if request.method == 'POST':
mainform = journalForm(request.POST)
subform = journalLineFormSet(request.POST)
if mainform.is_valid() and subform.is_valid():
instances = subform.save(commit=False)
for instance in instances:
instance.save()
mainform.save()


return HttpResponseRedirect('/myacc/posted/')

else:
##linesform = linesform(queryset=journal.objects.all().none())
mainform = journalForm()
subform = journalLineFormSet(queryset=journalLine.objects.all().none()) #prefix used if you want to set a cumstom prefix to from name

return render(request,'myacc/post.html','mainform': mainform, 'subform': subform)









share|improve this question
























  • UPDATE: I have added this in template subform.non_form_errors and it works but only for the first form instance , I mean it is not work with the 2nd form.. I have set extras to 2 and I want the validation to work with 2nd form too

    – ramezamr
    Mar 23 at 17:57

















0















I am using BaseModelFormSet and passed it to modelformset_factory in views.And inside BaseModelFormSet function i override clean() to make formset validation to rise validation error.The problem is that when I try to fill the formset with a wrong data, validation error is not showing at all



I have tried to render from in template manually with non field errors but did not work and went through documentation https://docs.djangoproject.com/en/2.1/topics/forms/modelforms/#overriding-clean-on-a-modelformset



UPDATE: if I intentionally filled the form with wrong data , it will validate only the first form in formset not all the fomrs (extras set to 2)



forms.py



class MyModelFormSet(BaseModelFormSet):
def clean(self):
super().clean()
if any(self.errors)
return

for form in self.forms:
debit = form.cleaned_data['debit']
if debit == 100:
raise forms.ValidationError('debit is 100!')
return debit


views.py



def postpage(request):
journalLineFormSet = modelformset_factory(journalLine,exclude=('journalNumber',),extra=2,can_delete=True,formset=MyModelFormSet,max_num=2,validate_max=True)
if request.method == 'POST':
mainform = journalForm(request.POST)
subform = journalLineFormSet(request.POST)
if mainform.is_valid() and subform.is_valid():
instances = subform.save(commit=False)
for instance in instances:
instance.save()
mainform.save()


return HttpResponseRedirect('/myacc/posted/')

else:
##linesform = linesform(queryset=journal.objects.all().none())
mainform = journalForm()
subform = journalLineFormSet(queryset=journalLine.objects.all().none()) #prefix used if you want to set a cumstom prefix to from name

return render(request,'myacc/post.html','mainform': mainform, 'subform': subform)









share|improve this question
























  • UPDATE: I have added this in template subform.non_form_errors and it works but only for the first form instance , I mean it is not work with the 2nd form.. I have set extras to 2 and I want the validation to work with 2nd form too

    – ramezamr
    Mar 23 at 17:57













0












0








0








I am using BaseModelFormSet and passed it to modelformset_factory in views.And inside BaseModelFormSet function i override clean() to make formset validation to rise validation error.The problem is that when I try to fill the formset with a wrong data, validation error is not showing at all



I have tried to render from in template manually with non field errors but did not work and went through documentation https://docs.djangoproject.com/en/2.1/topics/forms/modelforms/#overriding-clean-on-a-modelformset



UPDATE: if I intentionally filled the form with wrong data , it will validate only the first form in formset not all the fomrs (extras set to 2)



forms.py



class MyModelFormSet(BaseModelFormSet):
def clean(self):
super().clean()
if any(self.errors)
return

for form in self.forms:
debit = form.cleaned_data['debit']
if debit == 100:
raise forms.ValidationError('debit is 100!')
return debit


views.py



def postpage(request):
journalLineFormSet = modelformset_factory(journalLine,exclude=('journalNumber',),extra=2,can_delete=True,formset=MyModelFormSet,max_num=2,validate_max=True)
if request.method == 'POST':
mainform = journalForm(request.POST)
subform = journalLineFormSet(request.POST)
if mainform.is_valid() and subform.is_valid():
instances = subform.save(commit=False)
for instance in instances:
instance.save()
mainform.save()


return HttpResponseRedirect('/myacc/posted/')

else:
##linesform = linesform(queryset=journal.objects.all().none())
mainform = journalForm()
subform = journalLineFormSet(queryset=journalLine.objects.all().none()) #prefix used if you want to set a cumstom prefix to from name

return render(request,'myacc/post.html','mainform': mainform, 'subform': subform)









share|improve this question
















I am using BaseModelFormSet and passed it to modelformset_factory in views.And inside BaseModelFormSet function i override clean() to make formset validation to rise validation error.The problem is that when I try to fill the formset with a wrong data, validation error is not showing at all



I have tried to render from in template manually with non field errors but did not work and went through documentation https://docs.djangoproject.com/en/2.1/topics/forms/modelforms/#overriding-clean-on-a-modelformset



UPDATE: if I intentionally filled the form with wrong data , it will validate only the first form in formset not all the fomrs (extras set to 2)



forms.py



class MyModelFormSet(BaseModelFormSet):
def clean(self):
super().clean()
if any(self.errors)
return

for form in self.forms:
debit = form.cleaned_data['debit']
if debit == 100:
raise forms.ValidationError('debit is 100!')
return debit


views.py



def postpage(request):
journalLineFormSet = modelformset_factory(journalLine,exclude=('journalNumber',),extra=2,can_delete=True,formset=MyModelFormSet,max_num=2,validate_max=True)
if request.method == 'POST':
mainform = journalForm(request.POST)
subform = journalLineFormSet(request.POST)
if mainform.is_valid() and subform.is_valid():
instances = subform.save(commit=False)
for instance in instances:
instance.save()
mainform.save()


return HttpResponseRedirect('/myacc/posted/')

else:
##linesform = linesform(queryset=journal.objects.all().none())
mainform = journalForm()
subform = journalLineFormSet(queryset=journalLine.objects.all().none()) #prefix used if you want to set a cumstom prefix to from name

return render(request,'myacc/post.html','mainform': mainform, 'subform': subform)






django django-models django-forms django-templates django-views






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 24 at 22:11







ramezamr

















asked Mar 23 at 15:33









ramezamrramezamr

16




16












  • UPDATE: I have added this in template subform.non_form_errors and it works but only for the first form instance , I mean it is not work with the 2nd form.. I have set extras to 2 and I want the validation to work with 2nd form too

    – ramezamr
    Mar 23 at 17:57

















  • UPDATE: I have added this in template subform.non_form_errors and it works but only for the first form instance , I mean it is not work with the 2nd form.. I have set extras to 2 and I want the validation to work with 2nd form too

    – ramezamr
    Mar 23 at 17:57
















UPDATE: I have added this in template subform.non_form_errors and it works but only for the first form instance , I mean it is not work with the 2nd form.. I have set extras to 2 and I want the validation to work with 2nd form too

– ramezamr
Mar 23 at 17:57





UPDATE: I have added this in template subform.non_form_errors and it works but only for the first form instance , I mean it is not work with the 2nd form.. I have set extras to 2 and I want the validation to work with 2nd form too

– ramezamr
Mar 23 at 17:57












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55315390%2fdjango-validationerror-in-basemodelformset%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55315390%2fdjango-validationerror-in-basemodelformset%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript