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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:20:18+00:00 2026-06-09T15:20:18+00:00

I have a model where I want to use a class method to set

  • 0

I have a model where I want to use a class method to set the default of for a property:

class Organisation(db.Model):
    name=db.StringProperty()
    code=db.StringProperty(default=generate_code())

    @classmethod
    def generate_code(cls):
        import random
        codeChars='ABCDEF0123456789'
        while True: # Make sure code is unique
            code=random.choice(codeChars)+random.choice(codeChars)+\
                    random.choice(codeChars)+random.choice(codeChars)
            if not cls.all().filter('code = ',code).get(keys_only=True):
                return code

But I get a NameError:

NameError: name 'generate_code' is not defined

How can I access generate_code()?

  • 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-09T15:20:19+00:00Added an answer on June 9, 2026 at 3:20 pm

    As I said in a comment, I would use a classmethod to act as a factory and always create you entity through there. It keeps things simpler and no nasty hooks to get the behaviour you want.

    Here is a quick example.

    class Organisation(db.Model):
        name=db.StringProperty()
        code=db.StringProperty()
    
        @classmethod
        def generate_code(cls):
            import random
            codeChars='ABCDEF0123456789'
            while True: # Make sure code is unique
                code=random.choice(codeChars)+random.choice(codeChars)+\
                        random.choice(codeChars)+random.choice(codeChars)
                if not cls.all().filter('code = ',code).get(keys_only=True):
    
            return code
    
        @classmethod
        def make_organisation(cls,*args,**kwargs):
            new_org = cls(*args,**kwargs)
            new_org.code = cls.generate_code()
            return new_org
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class I model bind and I want to use output caching
I have model: [Validator(typeof(RegisterValidator))] public class RegisterModel { public string Name { get; set;
I have an abstract class that I use to set default values in a
I have a Photo model and I want to set up a form so
in a django-tastypie app I have the following Django-models: class Car(models.Model): name=models.CharField('name',max_length=64) class CarTrack(models.Model):
I use Cocoa Emacs 23.1.91 and I want to always have hl-line-mode turned off
If I have Model.objects.all() I want to get only one object for any content_object=foo,
I want to turn Model.attribute into Attribute.models. e.g. I currently have Model.first.attribute => string
I want to have a model with calculated fields that I can apply sorting
I have a rails model where I want to update an attribute by calculating

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.