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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:21:10+00:00 2026-06-10T15:21:10+00:00

Is it somehow possible to judge from a field instance whether it has been

  • 0

Is it somehow possible to judge from a field instance whether it has been defined in the base class (‘myBaseModel’) or in the sub class (‘myDerivedModel’).

Or other way round. Is it possible to get all non-inherited fields from a model instance?

For now I made my way to the field instance. Maybe the field instance has some meta information attached to it?

The setup:

class myBaseModel(models.Model):
    baseField = models.CharField(max_length=30)

    class Meta:
        abstract = True

class myDerivedModel(myBaseModel):
    subClassField = models.CharField(max_length=30)

The approach:

myModel = get_model('my_app_name', 'myDerivedModel')
defaultFields = myModel._meta.get_all_field_names()
for field in defaultFields:
    fieldInstance = myModel._meta.get_field_by_name(field)
    print fieldInstance[0]

This outputs subClassField and baseField. I m looking for the way to only output the subClassField.

from django/db/models/options.py

def get_field_by_name(self, name):
    """
    Returns the (field_object, model, direct, m2m), where field_object is
    the Field instance for the given name, model is the model containing
    this field (None for local fields), direct is True if the field exists
    on this model, and m2m is True for many-to-many relations. When
    'direct' is False, 'field_object' is the corresponding RelatedObject
    for this field (since the field doesn't have an instance associated
    with it).

    Uses a cache internally, so after the first access, this is very fast.
    """
  • 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-10T15:21:12+00:00Added an answer on June 10, 2026 at 3:21 pm
    inherited_fields=[]
    for base_class in list(myModel .__bases__):
        inherited_fields.extend(base_class._meta.get_all_field_names())
    ... your code
    for field in defaultFields:
        if field not in inherited_fields:
            do_something_with_non_inherited_fields()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If a class defined an annotation, is it somehow possible to force its subclass
Is it somehow possible to choose the super of a class (preferably in the
is it somehow possible to open a HTML popup window from within Flash and
Is it somehow possible to accesse a sesssion-scoped bean in a class extending the
Is it somehow possible to extend a type, wich is defined in another assembly,
Is that somehow possible to access one datastore? Or access one app from different
Given a Class<?> that describes class A , is it somehow possible to get
Is it somehow possible to create a solr document that contains sub-elements? For example,
Is it somehow possible to automatically generate a YAML schema file or models from
Is it somehow possible to test warnings i Ruby using RSpec? Like this: class

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.