-1

I have a Python class with different attributes but I do not seem to find how to get the type of each of them. I want to check whether if a given attribute has an specific type.

Be aware that I am talking about the class and not an instance.

Let's say that I have this class:

class SvnProject(models.Model):
    '''
        SVN Projects model
    '''
    shortname = models.CharField(
        verbose_name='Repository name',
        help_text='An unique ID for the SVN repository.',
        max_length=256,
        primary_key=True,
        error_messages={'unique':'Repository with this name already exists.'},
    )

How can I check whether shortname is a model.CharField or a model.Whatever?

djuarezg
  • 2,307
  • 2
  • 20
  • 34
  • 1
    Possible duplicate of [What's the canonical way to check for type in Python?](https://stackoverflow.com/questions/152580/whats-the-canonical-way-to-check-for-type-in-python) – Adelin Dec 21 '17 at 13:56
  • 1
    Possible duplicate of [how to get field type string from db model in django](https://stackoverflow.com/questions/20081924/how-to-get-field-type-string-from-db-model-in-django) – Dhia Dec 21 '17 at 13:59
  • `isinstance(SvnProject.shortname, models.CharField)`? – Stop harming Monica Dec 21 '17 at 14:06

2 Answers2

3
class Book:
    i = 0 # test class variable
    def __init__(self, title, price):
        self.title = title
        self.price = price
    ...

# let's check class variable type
# we can directly access Class variable by
# ClassName.class_variable with or without creating an object
print(isinstance(Book.i, int))
# it will print True as class variable **i** is an **integer**

book = Book('a', 1)

# class variable on an instance
print(isinstance(book.i, int))
# it will print True as class variable **i** is an **integer**

print(isinstance(book.price, float))
# it print False because price is integer

print(type(book.price))
# <type 'int'>

print(isinstance(book, Book))
# it will print True as book is an instance of class **Book**

For django related projects it would be like

from django.contrib.auth.models import User
from django.db.models.fields import AutoField

print(isinstance(User._meta.get_field('id'), AutoField))
# it will print True
Umar Asghar
  • 3,808
  • 1
  • 36
  • 32
  • He wants to inspect the class itself, not an _instance_ of the class. – John Gordon Dec 21 '17 at 14:01
  • I have mentioned for instance of class as well. See the answer I have updated already. – Umar Asghar Dec 21 '17 at 14:03
  • 1
    Also be aware that the OP is askig about a Django model class, which behaves differently in this regard compared to normal Python classes. See https://docs.djangoproject.com/en/2.0/ref/models/meta/#django.db.models.options.Options.get_field on how to access the field of a Django model. – Erik Cederstrand Dec 21 '17 at 14:04
  • 1
    `price` and `title` are instance attributes, which he _doesn't_ want. `Book` has no class attributes at all. – John Gordon Dec 21 '17 at 14:05
  • you can keep these variable either instance or class variables, but still type of the variable will be obtained the same way by using **type(..)** or **isinstance(..)** functions. like ```type(variable)``` or isinstance(variable, Class)```. – Umar Asghar Dec 21 '17 at 14:23
1

See https://docs.djangoproject.com/en/2.0/ref/models/meta/#django.db.models.options.Options.get_field

>>> SvnProject._meta.get_field('shortname')
<django.db.models.fields.CharField: shortname>
Erik Cederstrand
  • 9,643
  • 8
  • 39
  • 63