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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:54:55+00:00 2026-06-04T03:54:55+00:00

I am trying to set a default value for attribute threshold in this code,

  • 0

I am trying to set a default value for attribute threshold in this code, the threshold should be the current level*50 and this is the model

class Level (models.Model):
    name = models.CharField(max_length=20,null=True, blank=True)
    description = models.CharField(max_length=20, null=True, blank=True)
    number = models.IntegerField(null=True, blank=True)
    threshold = models.IntegerField(null=True, blank=True,default=50*number,editable=False)

i get an error unsupported operand types for * : 'int' and 'IntegerField'

  • 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-06-04T03:54:56+00:00Added an answer on June 4, 2026 at 3:54 am

    You best best is to do such calculation while saving the object. So override Model.save

    or a better generic way would be to write a custom field and override pre_save

    class DependentIntegerField(models.IntegerField):
    
        def pre_save(self, model_instance, add):
            if not add: # set the default only while adding model
                return super(self, DependentIntegerField).pre_save(model_instance, add)
    
            return model_instance.number*50
    

    You can further enhance it and make DependentIntegerField generic so that you can pass callable to it and do any calculation, and you can do further enhancements like checking if user has set the value or not before using default value, and to make it more generic so that you can make any Field as dependent field by passing the field class to a factory function. e.g.

    from django.db import models
    
    class_map = {}
    
    def depends_field_pre_save(self, model_instance, add):
        """
        if default is not callable or it is not a model add, lets skip our hook
        """
        if not add or not callable(self.default):
            super(self.__class__, self).__init__(self,*args, **kwargs)
        value = self.default(model_instance)
        setattr(model_instance, self.attname, value)
        return value
    
    def FieldDepends(field_class):
        """
        return a dervied class from field_class which supports dependent default
        """
        if field_class in class_map:
            # we already created this class so return that
            return class_map[field_class]
    
        new_class = type('Depends'+field_class.__name__, (field_class,), {'pre_save':depends_field_pre_save })
    
        class_map[field_class] = new_class
    
        return new_class
    

    and use it like this

    class DependentModel(models.Model):
    
        def threshold_default(model_instance=None):
            if model_instance is None:
                return 10
            return model_instance.number*10
    
        number = models.IntegerField(null=True, blank=True, default=10)
        threshold = FieldDepends(models.IntegerField)(null=True, blank=True, default=threshold_default,editable=False)
    

    I have created a small django project djangodepends on bitbucket with test cases

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

Sidebar

Related Questions

I'm trying to set a default value for a search field. The idea is
I am trying to set a number of Enums to default value I am
I'm trying to set the default value for a Content Taxonomy field in a
THIS QUESTION IS NOT ABOUT HOW TO SET DEFAULT VALUE OF A WIDGET Hello
Let's say I have a set of Django Models: class Article(models.Model): title = models.CharField(max_length=100,
I am trying to set default value in DateField using the following statement DateField
I'm trying to set a default value in an instance variable. I'm doing: module
I am trying to set default value to h:selectOneMenu. But, It is not working.
I'm trying to set the default value of a check box to be true
I’m creating a TextView dymacally and trying to set a default value that is

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.