0
0
Djangoframework~10 mins

String representation with __str__ in Django - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a __str__ method that returns the name of the model instance.

Django
class Product(models.Model):
    name = models.CharField(max_length=100)

    def __str__(self):
        return self.[1]
Drag options to blanks, or click blank then click option'
Aname
Bid
Cself
Dstr
Attempts:
3 left
💡 Hint
Common Mistakes
Returning self.id instead of self.name
Returning self without converting to string
Using a method name other than __str__
2fill in blank
medium

Complete the code to return a formatted string showing the user's full name.

Django
class User(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)

    def __str__(self):
        return f"[1] [2]"
Drag options to blanks, or click blank then click option'
Aself.first_name
Bfirst_name
Cself.last_name
Dlast_name
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names without self
Swapping first_name and last_name
3fill in blank
hard

Fix the error in the __str__ method to correctly return the email attribute.

Django
class Contact(models.Model):
    email = models.EmailField()

    def __str__(self):
        return [1]
Drag options to blanks, or click blank then click option'
Aemail
Bstr.email
Cself.email()
Dself.email
Attempts:
3 left
💡 Hint
Common Mistakes
Using email without self
Calling self.email as a function
4fill in blank
hard

Fill both blanks to return a string combining title and year attributes.

Django
class Movie(models.Model):
    title = models.CharField(max_length=100)
    year = models.IntegerField()

    def __str__(self):
        return f"[1] ([2])"
Drag options to blanks, or click blank then click option'
Aself.title
Bself.year
Ctitle
Dyear
Attempts:
3 left
💡 Hint
Common Mistakes
Using attribute names without self
Swapping title and year
5fill in blank
hard

Fill all three blanks to return a string showing full name and email.

Django
class Employee(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    email = models.EmailField()

    def __str__(self):
        return f"[1] [2] <[3]>"
Drag options to blanks, or click blank then click option'
Aself.first_name
Bself.last_name
Cself.email
Demail
Attempts:
3 left
💡 Hint
Common Mistakes
Using attribute names without self
Using email without self
Swapping order of attributes