xxxxxxxxxx
from django.db import models
class myModel(models.Model):
input1 = models.CharField(max_length=100)
input2 = models.TextField()
input3 - models.DateTimeField(auto_now=True)
def __str__(self):
return self.input1
xxxxxxxxxx
class Video(models.Model):
title = models.CharField(max_length=200, null=True, blank=True)
description = models.TextField(null=True, blank=True)
url = models.CharField(max_length=200, null=True, blank=True)
video_id = models.CharField(max_length=200, null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.title
Copy
xxxxxxxxxx
from django.db import models
class User(models.Model): # Abstract class
username = models.CharField(max_length=100)
dob = models.DateField()
sex = models.CharField(max_length=10)
# Override toString method : so that when we print object of this class, we can see the information like this
def __str__(self):
return f'Username: {self.username}, DOB: {self.dob}, Sex: {self.sex}'
class Teacher(User): # Teacher is also a user
position = models.CharField(max_length=100)
students = models.ManyToManyField('Student', related_name='teachers')
# Override toString method : so that when we print object of this class, we can see the information like this
def __str__(self):
return f'{super().__str__()}, Position: {self.position}'
class Student(User): # Student is also a user
club = models.CharField(max_length=100)
teachers = models.ManyToManyField('Teacher', related_name='students')
# Override toString method : so that when we print object of this class, we can see the information like this
def __str__(self):
return f'{super().__str__()}, Club: {self.club}'
class Course(models.Model):
name = models.CharField(max_length=100)
students = models.ManyToManyField(Student, through='Enrollment')
# Override toString method : so that when we print object of this class, we can see the information like this
def __str__(self):
return f'Course Name: {self.name}'
class Lesson(models.Model):
course = models.ForeignKey(Course, on_delete=models.CASCADE)
name = models.CharField(max_length=100)
# Override toString method : so that when we print object of this class, we can see the information like this
def __str__(self):
return f'Lesson Name: {self.name}, Course: {self.course.name}'
class Enrollment(models.Model):
course = models.ForeignKey(Course, on_delete=models.CASCADE)
student = models.ForeignKey(Student, on_delete=models.CASCADE)
# Ensure that they are composite key
class Meta:
unique_together = ('course', 'student')
# Override toString method : so that when we print object of this class, we can see the information like this
def __str__(self):
return f'Course: {self.course.name}, Student: {self.student.username}'
xxxxxxxxxx
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=100)
@classmethod
def create(cls, title):
book = cls(title=title)
# do something with the book
return book
book = Book.create("Pride and Prejudice")
xxxxxxxxxx
from django.db import models
class MyModel(models.Model):
name = models.CharField(max_length=100)
email = models.EmailField()
age = models.IntegerField()
created_at = models.DateTimeField(auto_now_add=True)
xxxxxxxxxx
from django.db import models
class Todo(models.Model):
title=models.CharField(max_length=150)
description=models.CharField(max_length=500)
completed=models.BooleanField(default=False)
# string representation of the class
def __str__(self):
#it will return the title
return self.title
xxxxxxxxxx
from django.db import models
class Author(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField()
class Publisher(models.Model):
name = models.CharField(max_length=300)
class Book(models.Model):
name = models.CharField(max_length=300)
pages = models.IntegerField()
price = models.DecimalField(max_digits=10, decimal_places=2)
rating = models.FloatField()
authors = models.ManyToManyField(Author)
publisher = models.ForeignKey(Publisher, on_delete=models.CASCADE)
pubdate = models.DateField()
class Store(models.Model):
name = models.CharField(max_length=300)
books = models.ManyToManyField(Book)