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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:21:24+00:00 2026-05-31T01:21:24+00:00

How can one accomplish class-based default value in following scheme? I mean, I would

  • 0

How can one accomplish class-based default value in following scheme? I mean, I would like to inherited classes set default value for “number” differently:

class OrderDocumentBase(PdfPrintable):
    number = models.PositiveIntegerField(default=self.create_number())

    @classmethod
    def create_number(cls):
        raise NotImplementedError

class Invoice(OrderDocumentBase):
    @classmethod
    def create_number(cls):
        return 1

class CreditAdvice(OrderDocumentBase):
    @classmethod
    def create_number(cls):
        return 2

I have looked at this stackoverflow question, but it doesn’t address the same problem. The only thing I thought would work was overloading OrderDocumentBase‘s __init__ method like this:

def __init__(self, *args, **kwargs):
    """
    Overload __init__ to enable dynamic set of default to number
    """
    super(OrderDocumentBase, self).__init__(*args, **kwargs)
    number_field = filter(lambda x: x.name == 'number', self._meta.fields)[0]
    number = self.__class__.create_number()
    number_field.default = number

This works, but only partially and behaves quite wierdly. In admin interface, I can see the default being set only after second or latter page refresh. On first try, None is being set 🙁

Second possibility is redefinition of number field in each class, but that doesn’t seem too much pretty. Is there any other way?

Can someone help?

  • 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-31T01:21:26+00:00Added an answer on May 31, 2026 at 1:21 am

    It does feel nicer to do this via default=, but anything you use there doesn’t have a way to get at your class or specific model. To have it show up properly in places like the admin, you could set it in init() instead of save().

    class OrderDocumentBase(PdfPrintable):
        number = models.PositiveIntegerField()
    
        def __init__(self, *args, **kwargs):
            super(OrderDocumentBase, self).__init__(*args, **kwargs)
            if not self.pk and not self.number:
                self.number = self.DEFAULT_NUMBER
    
    class Invoice(OrderDocumentBase):
        DEFAULT_NUMBER = 2
    
    class CreditAdvice(OrderDocumentBase):
        DEFAULT_NUMBER = 3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can one specify XML attribute values as CDATA ? If yes - what would
I am using Djangos default authentication system (django.contrib.auth) and I would like to add
How can I change XML-element name for field inherited from base class while doing
Initially I am navigating from one to another XAML class like this (for example,
One can accomplish same thing using different tools. So have I in examples below.
Can one place an arbitrary program (firefox, openoffice, etc...) in a QX11EmbedContainer? The fllowing
Can one of you smarties point me to some beginner content for converting an
Can one upload files to a domain other than the domain a script originates
How can one detect being in a chroot jail without root privileges? Assume a
How can one load custom (not an image, nor a sound file) resource file

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.