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

  • Home
  • SEARCH
  • 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 587571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:13:34+00:00 2026-05-13T15:13:34+00:00

I have a Django model for a sweater and want to be able to

  • 0

I have a Django model for a sweater and want to be able to enter the composition of materials used in the admin panel (say: “100% wool” or “50% wool, 50% cotton” or “50% wool, 45% cotton, 5% acryl”).

I have this model:

class Sweater(models.Model):
         wool   = models.IntegerField(max_length=3, default=100, verbose_name="wool (%)")
         cotton = models.IntegerField(max_length=3, default=0, verbose_name="cotton (%)")
         acryl  = models.IntegerField(max_length=3, default=0, verbose_name="acryl (%)")

How and where do I assert that the sum of the wool, cotton and acryl values must be 100 so that a user can’t enter for example “100% wool, 100% cotton, 100% acryl”?

  • 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-13T15:13:35+00:00Added an answer on May 13, 2026 at 3:13 pm

    You should likely do it in at least two places. One to make sure you don’t get incorrect data in the model and one to let the user know the sum does not add up to 100%. The below handles checking the sum during form cleaning:

    class SweaterForm(ModelForm):
        """Form for adding and updating sweaters."""
    
        def clean(self):
            cleaned_data = self.cleaned_data
            wool = cleaned_data.get('wool')
            cotton = cleaned_data.get('cotton')
            acryl = cleaned_data.get('acryl')
    
            # Check that the fabric composition adds up to 100%
            if not 'wool' in self._errors \
                    and not 'cotton' in self._errors \
                    and not 'acryl' in self._errors \
                    and (wool + cotton + acryl) != 100:
                msg = u"Composition must add up to 100%!"
                self._errors['wool'] = ErrorList([msg])
    
                # Remove field from the cleaned data as it is no longer valid
                del cleaned_data['wool']
    
            return cleaned_data
    
        class Meta:
            model = Sweater
    

    Hope that helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have django model which consist of parent child relationship filed I want to
I want a Django model to have a date field in which the year,
Lets say I have three django model classes - lets call them A, B
I have created a django model and used TextField for one of the attributes
I have FileField in my django model: file = models.FileField(upload_to=FOLDER_FILES_PATH) In Django admin section
My Django model has a field within which I want to have reference logic.
I have a reasonably complex custom Django model method. It's visible in the admin
Say I have django model that looks something like this: class Order(models.Model): number =
I have a django model Level for a game. class Level(models.Model): key = models.CharField(max_length=100)
Lets say I have some Django model that has a list of numbers as

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.