Reputation: 1
First I created a class of Employee, but when I access its property by class name, such as Employee.user_name, it raised an error, I do not know why:
from django.db import models
import uuid
class Employee(models.Model):
employee_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
user_name = models.CharField(max_length=30)
pwd = models.CharField(max_length=50)
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
email = models.EmailField()
entry_date = models.DateTimeField(auto_now=True, editable=False)
def __str__(self):
return self.user_name
the error is below:
>>> Employee.user_name
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: type object 'Employee' has no attribute 'user_name'
>>>
Upvotes: 0
Views: 62
Reputation: 245
Your class holds many items within it. You need to tell the model which instance you are wanting by calling it by "pk" or other unique field.
user = Employee.objects.get(pk=id)
user.user_name
Upvotes: 1