Django Model Inheritance Options Introduction - ORM Part-9

Описание к видео Django Model Inheritance Options Introduction - ORM Part-9

The Django ORM series covers a range of common functions that you will perform on a database with Django. In this tutorial we introduce the idea of model inheritance using 3 options that Django provides, Abstract models
Multi-table model inheritance and Proxy models. I give you an overview of each and provide a small example of how to use each type of inheritance option.

00:00 Introduction

Tutorial Series GitHub
https://github.com/veryacademy/django...

✨ Support us - join us as a very academy member
   / @veryacademy  

👍SUBSCRIBE to get more free tutorials, courses and code snippets!
   / @veryacademy  

👍👍Follow us on Facebook
  / veryacademycom-113232103670580  

👍👍👍Follow use on Twitter:
  / veryacademy  



from django.db import models
from django.utils import timezone

Abstract Model

class BaseItem(models.Model):
title = models.CharField(max_length=255)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)

class Meta:
abstract = True
ordering = ['title']

def __str__(self):
return self.title

class ItemA(BaseItem):
content = models.TextField()

class Meta(BaseItem.Meta):
ordering = ['-created']

class ItemB(BaseItem):
file = models.FileField(upload_to='files')

class ItemC(BaseItem):
file = models.FileField(upload_to='images')

class ItemD(BaseItem):
slug = models.SlugField(max_length=255, unique=True)


Multi-table model inheritance

class Books(models.Model):
title = models.CharField(max_length=100)
created = models.DateTimeField(auto_now_add=True)

class ISBN(Books):
books_ptr = models.OneToOneField(
Books, on_delete=models.CASCADE,
parent_link=True,
primary_key=True,
)
ISBN = models.TextField()


Proxy Model

class NewManager(models.Manager):
pass

class BookContent(models.Model):
title = models.CharField(max_length=100)
created = models.DateTimeField(auto_now_add=True)

class BookOrders(BookContent):
objects = NewManager()
class Meta:
proxy = True
ordering = ['created']

def created_on(self):
return timezone.now() - self.created

Комментарии

Информация по комментариям в разработке