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;
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
add a comment |
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
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
add a comment |
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
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
django django-models django-forms django-templates django-views
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
add a comment |
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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