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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:32:41+00:00 2026-06-09T21:32:41+00:00

So I can create Django model like this: from django.db import models class Something(models.Model):

  • 0

So I can create Django model like this:

from django.db import models

class Something(models.Model):
    title = models.TextField(max_length=200)

and I can work with it like this:

thing = Something()
#set title
thing.title = "First thing"
#get title
thing.title

All works as it should but I’d like to understand HOW it works.

title = models.TextField(max_length=200)

in non-Django Python code above line defines class variable title of type models.TextField and I could access it also like this: thing.__class__.title(link)

But in Django when I create instance of Something I suddenly have a title attribute where I can get/set text. And cannot access it with thing.__class__.title So clearly when doing thing.title I’m not accessing class variable “title” but some generated attribute/property, or?

I know that fields ended up in thing._meta.fields but how? What’s going on and how?

1, Does Django create property “title” behind the scenes?

2, What happened to class variable “title”?

  • 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-09T21:32:42+00:00Added an answer on June 9, 2026 at 9:32 pm

    I think its hard to beat what Django documentation has to say on this.

    The Model class (see base.py) has a metaclass attribute that defines ModelBase (also in base.py) as the class to use for creating new classes. So ModelBase.new is called to create this new Example class. It is important to realise that we are creating the class object here, not an instance of it. In other words, Python is creating the thing that will eventually be bound to the Example name in our current namespace.

    Basically a metaclass defines how a class itself will be created. During creation, additional attributes/methods/anything can be bound to that class. The example this stackoverflow answer gives, capitalizes all the attributes of a class

    # remember that `type` is actually a class like `str` and `int`
    # so you can inherit from it
    class UpperAttrMetaclass(type): 
        # __new__ is the method called before __init__
        # it's the method that creates the object and returns it
        # while __init__ just initializes the object passed as parameter
        # you rarely use __new__, except when you want to control how the object
        # is created.
        # here the created object is the class, and we want to customize it
        # so we override __new__
        # you can do some stuff in __init__ too if you wish
        # some advanced use involves overriding __call__ as well, but we won't
        # see this
        def __new__(upperattr_metaclass, future_class_name, 
                    future_class_parents, future_class_attr):
    
            attrs = ((name, value) for name, value in future_class_attr.items() if not name.startswith('__'))
            uppercase_attr = dict((name.upper(), value) for name, value in attrs)
    
            return type(future_class_name, future_class_parents, uppercase_attr)
    

    In a similar way, Django’s metaclass for Models can digest the attributes you’ve applied to the class and add various useful attributes for validation/etc, including even methods and what-not.

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

Sidebar

Related Questions

Given this model: from django.db import models from django.contrib.auth.admin import User # Create your
Say I have django model that looks something like this: class Order(models.Model): number =
I have the following multi-table inheritance situation: from django.db import Models class Partner(models.Model): #
I have two models like this: class A(models.Model): attachment = FileField(upload_to='a') class B(models.Model): attachment
I have a class Book : from django.db import models from users.models import User
I have an application fileman with models.py like so: from django.db import models from
I have a model that looks like this: class Item(models.Model): ... publish_date = models.DateTimeField(default=datetime.datetime.now)
I'm trying to create a Django model that handles the following: An Item can
I want a nice convenient attribute to do the following: from django.contrib.auth.models import User
Currently I have the from django.contrib.auth.models import User but I'm confused as to how

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.