4.2. Models Field String

  • models.CharField - stores a string of characters with maximum length (max: 255 characters)

  • models.TextField - stores a string of characters without maximum length

  • models.EmailField - stores an email address with validation

  • models.URLField - stores an URL address with validation

  • models.SlugField - stores a normalized string of characters (lowercase, no spaces, no special characters, etc.)

4.2.1. SetUp

>>> from django.core.validators import MinLengthValidator, MaxLengthValidator
>>> from django.db import models
>>> from django.utils.translation import gettext_lazy as _

4.2.2. Arguments

  • 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.2.3. CharField

  • CharField

>>> name = models.CharField(
...     verbose_name=_('name'),
...     max_length=30,
...     null=False,
...     blank=False,
...     db_index=True,
... )

4.2.4. TextField

  • TextField

>>> comment = models.TextField(
...     verbose_name=_('Comment'),
...     null=True,
...     blank=True,
...     default=None,
... )

4.2.5. EmailField

  • EmailField

>>> email = models.EmailField(
...     verbose_name=_('Email'),
...     null=False,
...     blank=False,
...     unique=True,
... )

4.2.6. URLField

  • URLField

>>> website = models.URLField(
...     verbose_name=_('Website'),
...     null=True,
...     blank=True,
...     default=None,
... )

4.2.7. SlugField

  • SlugField

>>> slug = models.SlugField(
...     verbose_name=_('Slug'),
...     editable=False,
...     db_index=True,
...     default=None,
... )

Slugify:

>>> from django.utils.text import slugify
>>>
>>> name = 'Apple MacBook Pro 13" SSD 4TB'
>>>
>>> slugify(name)
'apple-macbook-pro-13-ssd-4tb'
https://example.com/macbook-pro-13-ssd-4tb,12345

Usage:

>>> 
... class Product(models.Model):
...     name = models.CharField(verbose_name=_('Name'), max_length=255, db_index=True, default=None)
...     slug = models.SlugField(verbose_name=_('Slug'), editable=False, db_index=True, default=None)
...
...     def save(self, *args, **kwargs):
...         self.slug = slugify(self.name)
...         super().save(*args, **kwargs)

4.2.8. Validators

  • MinLengthValidator

  • MaxLengthValidator

>>> comment = models.TextField(
...     verbose_name=_('Comment'),
...     validators=[MinLengthValidator(2), MaxLengthValidator(20)],
...     null=True,
...     blank=True,
...     default=None,
... )

4.2.9. Use Case - 1

>>> 
... from django.db import models
... from django.utils.translation import gettext_lazy as _
... from django.utils.text import slugify
...
...
... class Customer(models.Model):
...     firstname = models.CharField(verbose_name=_('First Name'), max_length=50)
...     lastname = models.CharField(verbose_name=_('Last Name'), max_length=50, db_index=True)
...     username = models.SlugField(verbose_name=_('Username'), max_length=50, unique=True)
...     email = models.EmailField(verbose_name=_('Email'), max_length=100, blank=True, null=True, default=None)
...     website = models.URLField(verbose_name=_('Website'), blank=True, null=True, default=None)
...     comment = models.TextField(verbose_name=_('Comment'), blank=True, null=True, default=None)
...
...     def __str__(self):
...         return f'{self.firstname} {self.lastname}'
...
...     class Meta:
...         verbose_name = _('Customer')
...         verbose_name_plural = _('Customers')

4.2.10. 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 String
# - Difficulty: easy
# - Lines: 5
# - Minutes: 3

# %% English
# 0. Use `myproject.demo`
# 1. Create model `Person`
# 2. Add field `lastname` as `models.CharField`:
#    - verbose_name=_('Lastname')
#    - max_length=30
#    - null=False
#    - blank=False
#    - db_index=True
# 3. Add method `__str__` which returns `lastname`
# 4. Add class `Meta` with:
#    - app_label = 'demo'
#    - verbose_name = _('Person')
#    - verbose_name_plural = _('People')
#    - ordering = ['lastname']
# 5. Use `gettext_lazy`
# 6. Run `makemigrations`
# 7. Run `migrate`

# %% Polish
# 0. Użyj `myproject.demo`
# 1. Stwórz model `Person`
# 2. Dodaj pole `lastname` jako `models.CharField`:
#    - verbose_name=_('Lastname')
#    - max_length=30
#    - null=False
#    - blank=False
#    - db_index=True
# 3. Dodaj metodę `__str__` która zwraca `lastname`
# 4. Dodaj klasę `Meta` z:
#    - app_label = 'demo'
#    - verbose_name = _('Person')
#    - verbose_name_plural = _('People')
#    - ordering = ['lastname']
# 5. Użyj `gettext_lazy`
# 6. Uruchom `makemigrations`
# 7. Uruchom `migrate`

# %% Output
# - `Create model Person`
# - `Applying demo.0001_initial... 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):
    lastname = ...

# 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 String
# - Difficulty: easy
# - Lines: 1
# - Minutes: 3

# %% English
# 0. Use `myproject.demo`
# 1. Alter model `Person`
# 2. Add field `firstname` as `models.CharField`:
#    - verbose_name=_('Firstname')
#    - max_length=30
#    - null=True
#    - 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 `firstname` jako `models.CharField`:
#    - verbose_name=_('Firstname')
#    - max_length=30
#    - null=True
#    - blank=True
# 3. Użyj `gettext_lazy`
# 4. Uruchom `makemigrations`
# 5. Uruchom `migrate`

# %% Output
# - `Add field firstname to person`
# - `Applying demo.0003_person_firstname... 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):
    firstname = ...


# 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 String
# - Difficulty: easy
# - Lines: 1
# - Minutes: 3

# %% English
# 0. Use `myproject.demo`
# 1. Alter model `Person`
# 2. Add field `comment` as `models.TextField`:
#    - verbose_name=_('Comment')
#    - 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 `comment` jako `models.TextField`:
#    - verbose_name=_('Comment')
#    - null=True
#    - blank=True
#    - default=None
# 3. Użyj `gettext_lazy`
# 4. Uruchom `makemigrations`
# 5. Uruchom `migrate`

# %% Output
# - `Add field comment to person`
# - `Applying demo.0002_person_comment... 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):
    comment = ...


# 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 String
# - Difficulty: easy
# - Lines: 1
# - Minutes: 3

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

# %% Polish
# 0. Użyj `myproject.demo`
# 1. Zmień mode `Person`
# 2. Dodaj pole `email` jako `models.EmailField`:
#    - verbose_name=_('Email')
#    - unique=True
#    - null=True
#    - blank=True
#    - default=None
# 3. Użyj `gettext_lazy`
# 4. Uruchom `makemigrations`
# 5. Uruchom `migrate`

# %% Output
# - `Add field email to person`
# - `Applying demo.0003_person_email... 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):
    email = ...


# 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 String
# - Difficulty: easy
# - Lines: 1
# - Minutes: 3

# %% English
# 0. Use `myproject.demo`
# 1. Alter model `Person`
# 2. Add field `website` as `models.URLField`:
#    - verbose_name=_('Website')
#    - 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 `website` jako `models.URLField`:
#    - verbose_name=_('Website')
#    - null=True
#    - blank=True
#    - default=None
# 3. Użyj `gettext_lazy`
# 4. Uruchom `makemigrations`
# 5. Uruchom `migrate`

# %% Output
# - `Add field website to person`
# - `Applying demo.0004_person_website... 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):
    website = ...


# 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 String
# - Difficulty: easy
# - Lines: 2
# - Minutes: 3

# %% English
# 0. Use `myproject.demo`
# 1. Alter model `Person`
# 2. Add field `username` as `models.SlugField`:
#    - verbose_name=_('Username')
#    - null=True
#    - blank=True
#    - default=None
# 3. Define method `.save()`, which generates `username`
#    from first letter of a `firstname` and whole `lastname`
#    ie. Mark Watney => mwatney
# 4. Use `gettext_lazy`
# 5. Run `makemigrations`
# 6. Run `migrate`

# %% Polish
# 0. Użyj `myproject.demo`
# 1. Zmień model `Person`
# 2. Dodaj pole `username` jako `models.SlugField`:
#    - verbose_name=_('Username')
#    - null=True
#    - blank=True
#    - default=None
# 3. Zdefiniuj metodę `.save()`, która generuje `username`
#    z pierwszej litery `firstname` + całe `lastname`,
#    np. Mark Watney => mwatney
# 4. Użyj `gettext_lazy`
# 5. Uruchom `makemigrations`
# 6. Uruchom `migrate`

# %% Output
# - `Add field username to person`
# - `Applying demo.0005_person_username... 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):
    username = ...

    def save(self, *args, **kwargs):
        ...
        return super().save(*args, **kwargs)

# 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 String
# - Difficulty: easy
# - Lines: 2
# - Minutes: 3

# %% English
# 0. Use `myproject.demo`
# 1. Alter model `Person`
# 2. Alter field `comment`, add validators:
#    - MinLengthValidator(0)
#    - MaxLengthValidator(20)
# 3. Run `makemigrations`
# 4. Run `migrate`

# %% Polish
# 0. Użyj `myproject.demo`
# 1. Zmień model `Person`
# 2. Zmień pole `comment`, dodaj walidatory:
#    - MinLengthValidator(0)
#    - MaxLengthValidator(20)
# 3. Uruchom `makemigrations`
# 4. Uruchom `migrate`

# %% Output
# - `Alter field comment on person`
# - `Applying demo.0006_alter_person_comment... OK`

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

from django.core.validators import MinLengthValidator, MaxLengthValidator
from django.db import models
from django.utils.translation import gettext_lazy as _


class Person(models.Model):
    comment = models.TextField(verbose_name=_('Comment'), null=True, blank=True, default=None)