0

I am very confused after working with Django Framework and the model of Djano Framework

class User(models.Model):
    first_name = models.CharField(max_length=20)
    last_name  = models.CharField(max_length=20)
    email_address = models.EmailField(max_length=50, unique=True)

    def __str__(self):
        return self.first_name

As we can see first_name, last_name, and email_address are created as class variables but in the dunder str() method they are used with the self as instance variables

So my question is wheter in Python variables declared as class variables can be used as both "Class Variables" and "Instance Variables".

Thank You in advance and thanks for reading my question.

Mr. Suryaa Jha
  • 1,516
  • 16
  • 10
  • First, generally in Python, class variables are accessible from instances. Second, google the Python descriptor protocol. It allows classes to define instance behaviour when the instance is accessed as an attribute of an object. Model field classes implement this. – user2390182 Jan 04 '18 at 13:38
  • At some point, a class variable **becomes** an instance variable. – Adelin Jan 04 '18 at 13:38

0 Answers0