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 3460228
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:12:15+00:00 2026-05-18T10:12:15+00:00

This is a very simplified version of a model I’m working on: class ClothingTop(models.Model):

  • 0

This is a very simplified version of a model I’m working on:

class ClothingTop(models.Model):

    SWEATER  = 0
    SHIRT    = 1

    TOP_CHOICES = (
        (SWEATER, 'Sweat shirt'),
        (SHIRT, 'Shirt'),
    )

    name = models.CharField(max_length=32)
    type = models.PositiveSmallIntegerField(choices=TOP_CHOICES)
    hoodie = models.BooleanField(default=False)
    buttons = models.PositiveSmallIntegerField(null=True, blank=True)

    def __unicode__(self):
        return self.name

    @property
    def type_text(self):
        if self.type == self.SWEATER:
            if self.hoodie:
                return 'Hooded sweatshirt'
            return 'Plain sweatshirt'
        elif self.type == self.SHIRT:
            return 'Shirt'

I want to require buttons if the type is set to SHIRT. My first thought was to override the save method but I am not sure if that is necessarily the smartest way to accomplish this.

Anyone have any suggestions?

  • 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-18T10:12:15+00:00Added an answer on May 18, 2026 at 10:12 am

    My simplest suggestion, and I’d believe it’s the best one in practice, is that you create a ClothingTop ModelForm and set the buttons_clean() method on the form that will do the custom validation. This form would also have to be set for the ClothingTop ModelAdmin.

    The only other way to do it is by creating a custom model field for the buttons field (validators won’t work here because they only get the buttons field value and are oblivious to the type, other model fields). The easiest way to do it would be:

    ButtonsField(models.PositiveSmallIntegerField):
    
        def validate(self, value, model_instance):
            # here we get the buttons field value and can get the type value
            # exactly what we need!
    
            type = getattr(model_instance, 'type')
    
            if type == SHIRT and not value:
                raise ValidationError('Type set to shirt, but buttons value is empty')
    
            super(self, ButtonsField).validate(value, model_instance)
    

    I’ve mentioned the approach with custom fields for the sake of completeness, I think you should skip creating a custom field type unless it’s completely generic and readily reusable on any model. For those special cases, just use form validation. Your models should only ensure database integrity, which you’ve covered with ClothingTop perfectly already, business rules bubble down from form validation.

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

Sidebar

Related Questions

This is a (very) simplified version of my iPhone code : @interface x {
Have a look at this very simple example WPF program: <Window x:Class=WpfApplication1.Window1 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
i am trying to compile this very simple piece of code class myList {
This very simple code gives me tons of errors: #include <iostream> #include <string> int
I've written this very simple function to replace a file extension using LINQ in
Let's make this very easy. What I want: @array = qw/one two one/; my
I'm sorry for this very newbish question, I'm not much given into web development.
I find this very strange, must be something I'm doing wrong, but still... I'm
I was told this a few times in this very site, but I wanted
I seem to run into this very often. I need to build a Hash

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.