5.12. Models Method Staticmethod — Python
>>> # ... from django.db import models ... from django.utils.translation import gettext_lazy as _ ... ... ... class Customer(BaseModel): ... firstname = models.CharField(verbose_name=_('First Name'), max_length=100, null=False, blank=False, default=None) ... lastname = models.CharField(verbose_name=_('Last Name'), max_length=100, null=False, blank=False, default=None) ... birthdate = models.DateField(verbose_name=_('Birth Date'), null=True, blank=True, default=None) ... photo = models.ImageField(verbose_name=_('Photo'), upload_to='contact/', null=True, blank=True, default=None) ... ... @staticmethod ... def add(firstname, lastname, birthdate, photo): ... customer = Customer(firstname=firstname, lastname=lastname, birthdate=birthdate, photo=photo) ... customer.full_clean() ... customer.save() ... return customer ... ... def __str__(self): ... return f'{self.firstname} {self.lastname}' ... ... class Meta: ... verbose_name = _('Customer') ... verbose_name_plural = _('Customers')