2016-09-18 02:29:00 +03:00
|
|
|
from django.db import models
|
2020-04-06 15:21:07 +03:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2016-09-18 02:29:00 +03:00
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
CHOICES = ((1, "this"), (2, _("that")))
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
|
2020-12-31 08:03:57 +03:00
|
|
|
class Person(models.Model):
|
2024-01-30 12:09:18 +03:00
|
|
|
class Meta:
|
|
|
|
ordering = ["pk"]
|
|
|
|
|
2020-12-31 08:03:57 +03:00
|
|
|
name = models.CharField(max_length=30)
|
2024-01-20 11:36:00 +03:00
|
|
|
parent = models.ForeignKey(
|
|
|
|
"self", on_delete=models.CASCADE, null=True, blank=True, related_name="children"
|
|
|
|
)
|
2020-12-31 08:03:57 +03:00
|
|
|
|
|
|
|
|
2016-09-18 02:29:00 +03:00
|
|
|
class Pet(models.Model):
|
2024-01-30 12:09:18 +03:00
|
|
|
class Meta:
|
|
|
|
ordering = ["pk"]
|
|
|
|
|
2016-09-18 02:29:00 +03:00
|
|
|
name = models.CharField(max_length=30)
|
2018-07-23 01:34:01 +03:00
|
|
|
age = models.PositiveIntegerField()
|
2022-09-23 11:45:02 +03:00
|
|
|
owner = models.ForeignKey(
|
|
|
|
"Person", on_delete=models.CASCADE, null=True, blank=True, related_name="pets"
|
|
|
|
)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
|
|
|
|
class FilmDetails(models.Model):
|
|
|
|
location = models.CharField(max_length=30)
|
2018-07-20 02:51:33 +03:00
|
|
|
film = models.OneToOneField(
|
2023-08-09 20:28:26 +03:00
|
|
|
"Film",
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
related_name="details",
|
|
|
|
null=True,
|
|
|
|
blank=True,
|
2018-07-20 02:51:33 +03:00
|
|
|
)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
|
|
|
|
class Film(models.Model):
|
2024-01-30 12:09:18 +03:00
|
|
|
class Meta:
|
|
|
|
ordering = ["pk"]
|
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
genre = models.CharField(
|
|
|
|
max_length=2,
|
|
|
|
help_text="Genre",
|
2021-02-23 07:21:32 +03:00
|
|
|
choices=[("do", "Documentary"), ("ac", "Action"), ("ot", "Other")],
|
2018-07-20 02:51:33 +03:00
|
|
|
default="ot",
|
|
|
|
)
|
|
|
|
reporters = models.ManyToManyField("Reporter", related_name="films")
|
|
|
|
|
2016-09-18 02:29:00 +03:00
|
|
|
|
2017-12-12 20:52:32 +03:00
|
|
|
class DoeReporterManager(models.Manager):
|
|
|
|
def get_queryset(self):
|
2022-10-19 17:10:30 +03:00
|
|
|
return super().get_queryset().filter(last_name="Doe")
|
2016-09-18 02:29:00 +03:00
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
|
2016-09-18 02:29:00 +03:00
|
|
|
class Reporter(models.Model):
|
2024-01-30 12:09:18 +03:00
|
|
|
class Meta:
|
|
|
|
ordering = ["pk"]
|
|
|
|
|
2016-09-18 02:29:00 +03:00
|
|
|
first_name = models.CharField(max_length=30)
|
|
|
|
last_name = models.CharField(max_length=30)
|
|
|
|
email = models.EmailField()
|
2018-07-20 02:51:33 +03:00
|
|
|
pets = models.ManyToManyField("self")
|
2023-05-24 15:58:50 +03:00
|
|
|
a_choice = models.IntegerField(choices=CHOICES, null=True, blank=True)
|
2017-12-12 20:52:32 +03:00
|
|
|
objects = models.Manager()
|
|
|
|
doe_objects = DoeReporterManager()
|
2023-07-18 20:17:45 +03:00
|
|
|
fans = models.ManyToManyField(Person)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
2018-01-15 06:36:52 +03:00
|
|
|
reporter_type = models.IntegerField(
|
2018-07-20 02:51:33 +03:00
|
|
|
"Reporter Type",
|
2018-01-15 06:36:52 +03:00
|
|
|
null=True,
|
|
|
|
blank=True,
|
2020-04-06 15:21:07 +03:00
|
|
|
choices=[(1, "Regular"), (2, "CNN Reporter")],
|
2018-02-03 22:36:25 +03:00
|
|
|
)
|
2018-01-15 06:36:52 +03:00
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
def __str__(self): # __unicode__ on Python 2
|
2022-10-19 17:10:30 +03:00
|
|
|
return f"{self.first_name} {self.last_name}"
|
2016-09-18 02:29:00 +03:00
|
|
|
|
2018-01-15 06:36:52 +03:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Override the init method so that during runtime, Django
|
|
|
|
can know that this object can be a CNNReporter by casting
|
|
|
|
it to the proxy model. Otherwise, as far as Django knows,
|
|
|
|
when a CNNReporter is pulled from the database, it is still
|
|
|
|
of type Reporter. This was added to test proxy model support.
|
|
|
|
"""
|
2022-10-19 17:10:30 +03:00
|
|
|
super().__init__(*args, **kwargs)
|
2018-01-15 06:36:52 +03:00
|
|
|
if self.reporter_type == 2: # quick and dirty way without enums
|
|
|
|
self.__class__ = CNNReporter
|
|
|
|
|
2019-12-31 16:55:45 +03:00
|
|
|
def some_method(self):
|
|
|
|
return 123
|
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
|
2019-03-27 07:09:25 +03:00
|
|
|
class CNNReporterManager(models.Manager):
|
|
|
|
def get_queryset(self):
|
2022-10-19 17:10:30 +03:00
|
|
|
return super().get_queryset().filter(reporter_type=2)
|
2019-03-27 07:09:25 +03:00
|
|
|
|
|
|
|
|
2018-01-15 06:36:52 +03:00
|
|
|
class CNNReporter(Reporter):
|
|
|
|
"""
|
|
|
|
This class is a proxy model for Reporter, used for testing
|
|
|
|
proxy model support
|
|
|
|
"""
|
2018-07-20 02:51:33 +03:00
|
|
|
|
2018-01-15 06:36:52 +03:00
|
|
|
class Meta:
|
|
|
|
proxy = True
|
|
|
|
|
2019-03-27 07:09:25 +03:00
|
|
|
objects = CNNReporterManager()
|
|
|
|
|
2016-09-18 02:29:00 +03:00
|
|
|
|
2023-07-18 20:17:45 +03:00
|
|
|
class APNewsReporter(Reporter):
|
|
|
|
"""
|
2023-09-06 10:29:58 +03:00
|
|
|
This class only inherits from Reporter for testing multi table inheritance
|
2023-07-18 20:17:45 +03:00
|
|
|
similar to what you'd see in django-polymorphic
|
|
|
|
"""
|
|
|
|
|
|
|
|
alias = models.CharField(max_length=30)
|
|
|
|
objects = models.Manager()
|
|
|
|
|
|
|
|
|
2016-09-18 02:29:00 +03:00
|
|
|
class Article(models.Model):
|
|
|
|
headline = models.CharField(max_length=100)
|
2021-02-23 07:21:32 +03:00
|
|
|
pub_date = models.DateField(auto_now_add=True)
|
|
|
|
pub_date_time = models.DateTimeField(auto_now_add=True)
|
2018-07-20 02:51:33 +03:00
|
|
|
reporter = models.ForeignKey(
|
|
|
|
Reporter, on_delete=models.CASCADE, related_name="articles"
|
|
|
|
)
|
|
|
|
editor = models.ForeignKey(
|
|
|
|
Reporter, on_delete=models.CASCADE, related_name="edited_articles_+"
|
|
|
|
)
|
|
|
|
lang = models.CharField(
|
|
|
|
max_length=2,
|
|
|
|
help_text="Language",
|
|
|
|
choices=[("es", "Spanish"), ("en", "English")],
|
|
|
|
default="es",
|
|
|
|
)
|
|
|
|
importance = models.IntegerField(
|
|
|
|
"Importance",
|
|
|
|
null=True,
|
|
|
|
blank=True,
|
2020-04-06 15:21:07 +03:00
|
|
|
choices=[(1, "Very important"), (2, "Not as important")],
|
2018-07-20 02:51:33 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
def __str__(self): # __unicode__ on Python 2
|
2016-09-18 02:29:00 +03:00
|
|
|
return self.headline
|
|
|
|
|
|
|
|
class Meta:
|
2018-07-20 02:51:33 +03:00
|
|
|
ordering = ("headline",)
|