16. Model Use Case

16.1. Use Case - Contact

import datetime

from django.db import models
from django.utils.translation import gettext_lazy as _


class Contact(models.Model):
    STATUS_BEST_FRIEND = 'best-friend'
    STATUS_FRIEND = 'friend'
    STATUS_ACQUAINTANCE = 'acquaintance'
    STATUS_OTHER = 'other'
    STATUS_CHOICES = [
        (STATUS_BEST_FRIEND, _('Best Friend')),
        (STATUS_FRIEND, _('Friend')),
        (STATUS_ACQUAINTANCE, _('Acquaintance')),
        (STATUS_OTHER, _('Other'))]

    GENDER_MALE = 'male'
    GENDER_FEMALE = 'female'
    GENDER_OTHER = None
    GENDER_CHOICES = [
        (GENDER_MALE, _('Male')),
        (GENDER_FEMALE, _('Female')),
        (GENDER_OTHER, _('Undisclosed'))]

    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)
    reporter = models.ForeignKey(verbose_name=_('Reporter'), to='auth.User', on_delete=models.CASCADE, null=True, default=None)
    is_deleted = models.BooleanField(verbose_name=_('Is deleted?'), default=False)

    firstname = models.CharField(verbose_name=_('First Name'), max_length=30)
    lastname = models.CharField(verbose_name=_('Last Name'), max_length=30, db_index=True)
    date_of_birth = models.DateField(verbose_name=_('Date of birth'), null=True, blank=True, default=None)
    email = models.EmailField(verbose_name=_('Email'), null=True, blank=True, default=None)
    bio = models.TextField(verbose_name=_('Bio'), null=True, blank=True, default=None)
    image = models.ImageField(verbose_name=_('Image'), null=True, blank=True, default=None)
    status = models.CharField(verbose_name=_('Status'), max_length=30, choices=STATUS_CHOICES, null=True, blank=True, default=None)
    gender = models.CharField(verbose_name=_('Gender'), max_length=30, choices=GENDER_CHOICES, null=True, blank=True, default=None)
    friends = models.ManyToManyField(verbose_name=_('Friends'), to='contact.Contact', blank=True, default=None)

    def __str__(self):
        return f'{self.firstname} {self.lastname}'

    def get_age(self):
        if not self.date_of_birth:
            return None

        today = datetime.date.today()
        return today.year - self.date_of_birth.year


    def save(self, *args, **kwargs):
        # is called at Model.save()
        return super().save(*args, **kwargs)

    class Meta:
        verbose_name = _('Contact')
        verbose_name_plural = _('Contacts')
        unique_together = ['firstname', 'lastname']

16.2. Assignments

Code 16.9. Solution
"""
* Assignment: Django Models FlowerShop
* Complexity: medium
* Lines of code: 50 lines
* Time: 21 min

English:
    1. Create project `flowershop`
    2. Create app `flower`
    3. Create model `Iris` with fields:
        a. sepal_length
        b. sepal_width
        c. petal_length
        d. petal_width
        e. species (with choices)
        f. image
    4. Generate admin panel
    5. Add search by species
    6. Add filter by species
    7. Run doctests - all must succeed

Polish:
    1. Stwórz projekt `flowershop`
    2. Stwórz apkę `flower`
    3. Stwórz model `Iris` z polami:
        a. sepal_length
        b. sepal_width
        c. petal_length
        d. petal_width
        e. species (z choices)
        f. image
    4. Wygeneruj panel administracyjny
    5. Dodaj wyszukiwarkę po gatunku
    6. Dodaj filtrowanie po nazwie gatunku
    7. Uruchom doctesty - wszystkie muszą się powieść
"""


../../_images/iris-setosa.jpg

Figure 16.8. Iris Setosa 1

../../_images/iris-versicolor.jpg

Figure 16.9. Iris Versicolor 2

../../_images/iris-virginica.jpg

Figure 16.10. Iris Virginica 3

Code 16.10. Solution
"""
* Assignment: Django Models AddressBook
* Complexity: medium
* Lines of code: 50 lines
* Time: 21 min

English:
    TODO: English Translation
    X. Run doctests - all must succeed

Polish:
    1. Stwórz projekt `addressbook`
    2. Stwórz apkę `contact`
    3. Stwórz model `Address` z polami:
        a. Typ (do wyboru typ: domowy, praca, komórka)
        b. Ulica
        c. Numer bloku
        d. Numer mieszkania
        e. Kod pocztowy
        f. Miasto
        g. Region
        h. Kraj
    4. Stwórz model `Person` z polami:
        a. Imię
        b. Nazwisko
        c. Data Urodzenia
        d. Zdjęcie
        e. Telefon (do wyboru typ: domowy, praca, komórka)
        f. Email (do wyboru typ: domowy, praca, komórka)
        g. Adres
    5. Osoba może mieć wiele adresów, telefonów i e-maili
    6. Wygeneruj panel administracyjny
    7. Można wypisać kontakty i na głównym ekranie widoczne są podstawowe pola osoby
    8. Dodaj wyszukiwarkę po nazwisku
    9. Dodaj filtrowanie po dacie urodzenia
    10. Uruchom doctesty - wszystkie muszą się powieść
"""


16.3. References

1

https://commons.wikimedia.org/wiki/File:Iris_setosa_2.jpg

2

Blue flag flower (Iris versicolor). Photo taken by Danielle Langlois in July 2005 at the Forillon National Park of Canada, Quebec, Canada. https://commons.wikimedia.org/wiki/File:Iris_versicolor_2.jpg

3

https://commons.wikimedia.org/wiki/File:Iris_virginica.jpg