Skip to content

Commit 68f900b

Browse files
committed
Add new __str__ methods to models
- This MR adds new __str__ methods to the class to customize the string representation of the objects.
1 parent ddf4206 commit 68f900b

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

apidemo/test_app/models.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
from django.db import models
22

3+
34
# Create your models here.
45
# The Department class is a model that represents a department with a title attribute.
56
class Department(models.Model):
67
title = models.CharField(max_length=100)
78

9+
def __str__(self) -> str:
10+
return f"{self.title}"
11+
812
# The Employee class represents an employee with attributes such as first name, last name, department,
913
# and birthdate.
1014
class Employee(models.Model):
1115
first_name = models.CharField(max_length=100)
1216
last_name = models.CharField(max_length=100)
1317
department = models.ForeignKey(Department, on_delete=models.CASCADE)
1418
birthdate = models.DateField(null=True, blank=True)
19+
20+
def __str__(self) -> str:
21+
return f"{self.first_name} {self.last_name}"

0 commit comments

Comments
 (0)