4.8. Models Field Relation

  • models.ForeignKeyField - represents many-to-one relationship, requires two arguments: to and on_delete

  • models.OneToOneField - represents one-to-one relationship, requires two arguments: to and on_delete

  • models.ManyToManyField - represents many-to-many relationship, requires one argument: to

>>> 
... from django.db import models
... from django.utils.translation import gettext_lazy as _
...
...
... class Phone(models.Model):
...     customer = models.ForeignKey(verbose_name=_('Customer'), to='contact.Customer', blank=False, null=None, default=None, on_delete=models.CASCADE)
...     number = models.CharField(verbose_name=_('Phone Number'), max_length=15, blank=False, null=True, default=None)
...
...     def __str__(self):
...         return f'{self.number}'
...
...     class Meta:
...         verbose_name = _('Phone')
...         verbose_name_plural = _('Phones')
...
...
... class Role(models.Model):
...     name = models.CharField(verbose_name=_('Name'), max_length=20, null=False, blank=False)
...
...     def __str__(self):
...         return f'{self.name}'
...
...     class Meta:
...         verbose_name = _('Role')
...         verbose_name_plural = _('Roles')
...
...
... class Customer(models.Model):
...     firstname = models.CharField(verbose_name=_('First Name'), max_length=100, null=False, blank=False)
...     lastname = models.CharField(verbose_name=_('Last Name'), max_length=100, null=False, blank=False)
...     roles = models.ManyToManyField(verbose_name=_('Roles'), to='demo.Role', blank=True)
...     groups = models.ManyToManyField(verbose_name=_('Group'), to='auth.Group', limit_choices_to={'name__startswith': 'customer_'}, blank=True)
...
...     def __str__(self):
...         return f'{self.firstname} {self.lastname}'
...
...     class Meta:
...         verbose_name = _('Customer')
...         verbose_name_plural = _('Customers')

4.8.1. Arguments

  • on_delete (ForeignKey, ManyToManyField)

  • related_name (ForeignKey, ManyToManyField)

  • to (ForeignKey, ManyToManyField)

  • limit_choices_to (ForeignKey, ManyToManyField)

  • blank

  • choices

  • db_column

  • db_index

  • default

  • editable

  • error_message

  • help_text

  • limit_choices_to

  • max_length

  • null

  • primary_key

  • unique

  • validators

  • verbose_name

4.8.2. Assignments

# TODO: Create Tests
# doctest: +SKIP_FILE
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% About
# - Name: Django Model Relations
# - Difficulty: easy
# - Lines: 1
# - Minutes: 3

# %% English
# 0. Use `myproject.demo`
# 1. Alter model `Person`
# 2. Add field `o2o` as `models.OneToOneField`:
#    - verbose_name=_('One To One')
#    - to='...'
#    - null=True
#    - blank=True
#    - default=None
# 3. Use `gettext_lazy`
# 4. Run `makemigrations`
# 5. Run `migrate`

# %% Polish
# 0. Użyj `myproject.demo`
# 1. Zmień model `Person`
# 2. Dodaj pole `o2o` jako `models.OneToOneField`:
#    - verbose_name=_('One To One')
#    - to='...'
#    - null=True
#    - blank=True
#    - default=None
# 3. Użyj `gettext_lazy`
# 4. Uruchom `makemigrations`
# 5. Uruchom `migrate`

# %% Output
# - `Add field o2o to person`
# - `Applying demo.0007_person_o2o... OK`

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 10), \
'Python 3.10+ required'
"""

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


class Person(models.Model):
    o2o = ...

# TODO: Create Tests
# doctest: +SKIP_FILE
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% About
# - Name: Django Model Relations
# - Difficulty: easy
# - Lines: 1
# - Minutes: 3

# %% English
# 0. Use `myproject.demo`
# 1. Alter model `Person`
# 2. Add field `roles` as `models.ManyToManyField`:
#    - verbose_name=_('Roles')
#    - blank=True
# 3. Use `gettext_lazy`
# 4. Run `makemigrations`
# 5. Run `migrate`

# %% Polish
# 0. Użyj `myproject.demo`
# 1. Zmień model `Person`
# 2. Dodaj pole `roles` jako `models.ManyToManyField`:
#    - verbose_name=_('Roles')
#    - blank=True
# 3. Użyj `gettext_lazy`
# 4. Uruchom `makemigrations`
# 5. Uruchom `migrate`

# %% Output
# - `Add field roles to person`
# - `Applying demo.0007_person_roles... OK`

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 10), \
'Python 3.10+ required'
"""

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


class Person(models.Model):
    roles = ...

# TODO: Create Tests
# doctest: +SKIP_FILE
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% About
# - Name: Django Model Relations
# - Difficulty: easy
# - Lines: 1
# - Minutes: 3

# %% English
# 0. Use `myproject.demo`
# 1. Alter model `Person`
# 2. Add field `groups` as `models.ManyToManyField`:
#    - verbose_name=_('Groups')
#    - limit_choices_to={'name__startswith': 'customer_'}
#    - blank=True
# 3. Use `gettext_lazy`
# 4. Run `makemigrations`
# 5. Run `migrate`

# %% Polish
# 0. Użyj `myproject.demo`
# 1. Zmień model `Person`
# 2. Dodaj pole `groups` jako `models.ManyToManyField`:
#    - verbose_name=_('Groups')
#    - limit_choices_to={'name__startswith': 'customer_'}
#    - blank=True
# 3. Użyj `gettext_lazy`
# 4. Uruchom `makemigrations`
# 5. Uruchom `migrate`

# %% Output
# - `Add field groups to person`
# - `Applying demo.0007_person_groups... OK`

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 10), \
'Python 3.10+ required'
"""

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


class Person(models.Model):
    groups = ...

# TODO: Create Tests
# doctest: +SKIP_FILE
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% About
# - Name: Django Model Relations
# - Difficulty: easy
# - Lines: 1
# - Minutes: 3

# %% English
# 0. Use `myproject.demo`
# 1. Alter model `Address`
# 2. Add field `person` as `models.ForeignKey`:
#    - verbose_name=_('Person')
#    - on_delete=models.CASCADE
#    - null=True
#    - blank=True
# 3. Use `gettext_lazy`
# 4. Run `makemigrations`
# 5. Run `migrate`

# %% Polish
# 0. Użyj `myproject.demo`
# 1. Zmień model `Address`
# 2. Dodaj pole `person` jako `models.ForeignKey`:
#    - verbose_name=_('Person')
#    - on_delete=models.CASCADE
#    - null=True
#    - blank=True
# 3. Użyj `gettext_lazy`
# 4. Uruchom `makemigrations`
# 5. Uruchom `migrate`

# %% Output
# - `Add field person to address`
# - `Applying demo.0007_address_person... OK`

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 10), \
'Python 3.10+ required'
"""

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


class Address(models.Model):
    street = models.CharField(verbose_name=_('Street'), max_length=20, null=True, blank=True, default=None)
    city = models.CharField(verbose_name=_('City'), max_length=20, null=False, blank=False)
    postcode = models.CharField(verbose_name=_('Postcode'), max_length=20, null=True, blank=True, default=None)
    country = models.CharField(verbose_name=_('Country'), max_length=20, null=False, blank=False)

    def __str__(self):
        return f'{self.city}, {self.country}'

    class Meta:
        app_label = 'demo'
        ordering = ['country', 'city']
        verbose_name = _('Address')
        verbose_name_plural = _('Addresses')


class Person(models.Model):
    firstname = models.CharField(verbose_name=_('Firstname'), max_length=30, null=True, blank=True, default=None)
    lastname = models.CharField(verbose_name=_('Lastname'), max_length=30, null=False, blank=False)

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

    class Meta:
        app_label = 'demo'
        verbose_name = _('Person')
        verbose_name_plural = _('People')
        ordering = ['lastname', 'firstname']