Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4052874
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:22:29+00:00 2026-05-20T14:22:29+00:00

I am trying to clean my inline formset data. I have three rules: 1)

  • 0

I am trying to clean my inline formset data. I have three rules: 1) must supply at least one name, 2) if you specify a name you must specify both a first and last, and 3) no spaces. When the data saves, the spaces are still saved to the database. What am I doing wrong?

Another issue is that the Validation Errors are not displayed. Why is that?

forms.py:
class UserNameForm(forms.models.BaseInlineFormSet):
    def clean(self):
        super(UserNameForm, self).clean()
        count = 0
        for form in self.forms:
            try:
                firstName = form.cleaned_data.get("name_first")
                middleName = form.cleaned_data.get("name_middle")
                lastName = form.cleaned_data.get("name_last")
                if firstName or middleName or lastName:
                    if len(firstName) == 0 or len(lastName) == 0:
                        raise forms.ValidationError("First and Last name are required.")

                if form.cleaned_data and not form.cleaned_data.get('DELETE', False):
                    count += 1
                    form.cleaned_data['name_first'] = firstName.replace(" ",'')
                    form.cleaned_data["name_middle"] = middleName.replace(" ",'')
                    form.cleaned_data["name_last"] = lastName.replace(" ",'')
            except AttributeError:
                pass
        if count < 1:
            raise forms.ValidationError('You must have at least one order')

views.py
UserNameFormSet = inlineformset_factory(Profile, Alias, formset=UserNameForm, extra=2, can_delete=True )
if request.method == 'POST':
    cNameFormSet = UserNameFormSet(request.POST, request.FILES, instance=cProfile)
    if cNameFormSet.is_valid():
        testResults  = cNameFormSet.save(commit=False)
        for form in testResults:
            form.save()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T14:22:29+00:00Added an answer on May 20, 2026 at 2:22 pm

    You can apply both a form and formset. Use the formset to validate data across the forms and the form to validate data within the form and raise validation errors.

    UserNameFormSet = inlineformset_factory(Profile,
                                            Alias,
                                            form=UserNameForm,
                                            formset=AtLeastOne,
                                            extra=2,
                                            can_delete=True )
    
    class AtLeastOne(forms.models.BaseInlineFormSet):
        def clean(self):
            count = 0
            for form in self.forms:
                try:
                    if form.cleaned_data and not form.cleaned_data.get('DELETE', False):
                        count += 1
                except AttributeError:
                    pass
            if count < 1:
                raise forms.ValidationError('You must have at least one name')
    
    class UserNameForm(forms.ModelForm):
       class Meta:
            model = Alias
       def clean(self):
           cleaned_data = self.cleaned_data
           # Check 1: Must have first and last.
           firstName = cleaned_data.get("name_first")
           middleName = cleaned_data.get("name_middle")
           lastName = cleaned_data.get("name_last")
           if firstName or middleName or lastName:
               if len(firstName) == 0 or len(lastName) == 0:
                   raise forms.ValidationError("First and Last name are required.")
           # Check 2: No spaces.
           cleaned_data["name_first"] = firstName.replace(" ",'')
           cleaned_data["name_middle"] = middleName.replace(" ",'')
           cleaned_data["name_last"] = lastName.replace(" ",'')
           return cleaned_data
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to get the WordPress menu inline and below my side
Trying to clean up some code. I have a button that has the following
I was trying to clean up some accessability stuff in my code, and inadvertently
I am trying to clean up some email addresses in sql server. I can
I'm trying to create clean URLs on my website. Now I succeeded in configuring
I'm trying to come up with a clean way of sorting a set of
I am trying to figure out a clean way to intercept uncaught exceptions that
I have been trying to set an image on an HTML.ActionLink by adding a
I´m trying to clean the factor variables in a dataframe from trailing spaces. However
I am trying to clean up some HTML values that are imported from other

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.