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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:32:43+00:00 2026-06-12T12:32:43+00:00

models.py: from django.db import models class Blog(models.Model): title = models.CharField(max_length=100, unique=True) body = models.TextField()

  • 0

models.py:

from django.db import models

class Blog(models.Model):
    title = models.CharField(max_length=100, unique=True)
    body = models.TextField()

Calling the dir function on a class in Python normally shows a list that includes class variables if they are defined. But strangely when I do python manage.py shell and do the following:

>>> import blog.models as bm
>>> dir(bm.Blog)
['DoesNotExist', 'MultipleObjectsReturned', '__class__', '__delattr__', '__dict__', 
'__doc__', '__eq__', '__format__', '__getattribute__', '__hash__', '__init__', 
'__metaclass__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', 
'__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__unicode__', 
'__weakref__', '_base_manager', '_default_manager', '_deferred', '_get_FIELD_display', 
'_get_next_or_previous_by_FIELD', '_get_next_or_previous_in_order', '_get_pk_val', 
'_get_unique_checks', '_meta', '_perform_date_checks', '_perform_unique_checks', 
'_set_pk_val', 'clean', 'clean_fields', 'date_error_message', 'delete', 'full_clean', 
'objects', 'pk', 'prepare_database_save', 'save', 'save_base', 'serializable_value', 
'unique_error_message', 'validate_unique']

dir(bm.Blog) does not contain the title and body class variables (unexpected). dir(bm.Blog()) however does contain those class variables (expected).

>>> dir(bm.Blog())
['DoesNotExist', 'MultipleObjectsReturned', '__class__', '__delattr__', '__dict__', 
'__doc__', '__eq__', '__format__', '__getattribute__', '__hash__', '__init__', 
'__metaclass__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', 
'__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__unicode__', 
'__weakref__', '_base_manager', '_default_manager', '_deferred', '_get_FIELD_display', 
'_get_next_or_previous_by_FIELD', '_get_next_or_previous_in_order', '_get_pk_val', 
'_get_unique_checks', '_meta', '_perform_date_checks', '_perform_unique_checks', 
'_set_pk_val', '_state', 'body', 'clean', 'clean_fields', 'date_error_message', 
'delete', 'full_clean', 'id', 'objects', 'pk', 'prepare_database_save', 'save', 
'save_base', 'serializable_value', 'title', 'unique_error_message', 'validate_unique']

So why can’t I see the class variables of a Django Model subclass when I call the dir function on that class?

  • 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-12T12:32:44+00:00Added an answer on June 12, 2026 at 12:32 pm

    This happens because you have inherited from db.Model, which has a specific metaclass ModelBase. So title=models.CharField and other fields (that provides meta information about rows in your table) are not real attributes of the model-class, instead of values of this fields in model-instance. But you can get access to them:

    Blog._meta.get_field('title') # models.CharField instance
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

from django.db import models class Post(models.Model): title = models.CharField(max_length=100) content = models.CharField(max_length=1000) created =
from django.db import models from django.contrib.auth.models import User class Product(models.Model): name = models.CharField(max_length =
Given a Django model likeso: from django.db import models class MyModel(models.Model): textfield = models.TextField()
In my models.py: from django.db import models from core import tasks class Image(models.Model): image
Let's assume we have following models: from django.db import models class Foo(models.Model): name =
Greetings, I have these 2 models: from django.db import models class Office(models.Model): name =
Consider the following django model from django.db import models from django.contrib import auth class
class FileUploadModel(models.Model): title = models.CharField(max_length=50) uploaded_file = models.FileField(upload_to = 'bose') Error: Traceback (most recent
Code: class JobsManager(models.Manager): def get_active_by_category(self, cat, limit): import datetime from django.db.models import Q return
I have a simple test case failing in Django: Model (app/models.py): from django.db import

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.