Skip to content

Models

Collection of model.

CustomUser (AbstractUser) django-model

Reference user model.

Source code in users/models.py
class CustomUser(AbstractUser):
    """Reference user model."""

    id = models.UUIDField(  # noqa: A003
        default=uuid.uuid4,
        editable=False,
        verbose_name=_("id"),
        primary_key=True,
    )

    email = models.EmailField(verbose_name=_("email address"), unique=True)

    full_name = models.CharField(verbose_name=_("full name"), max_length=300)

    picture = models.ImageField(
        verbose_name=_("picture"),
        default="images/default/pic.png",
        upload_to=user_upload_to,
    )

    class Meta:
        """Meta data."""

        verbose_name = _("profile")
        verbose_name_plural = _("profiles")

    def __str__(self: "CustomUser") -> str:
        """It returns readable name for the model."""
        return f"{self.username}"

date_joined: DateTimeField django-field

date joined

email: EmailField django-field

email address

first_name: CharField blank django-field

first name

full_name: CharField django-field

full name

groups: ManyToManyField blank django-field

id: UUIDField django-field

id

is_active: BooleanField django-field

is_staff: BooleanField django-field

staff status: Designates whether the user can log into this admin site.

is_superuser: BooleanField django-field

superuser status: Designates that this user has all permissions without explicitly assigning them.

last_login: DateTimeField blank django-field nullable

last login

last_name: CharField blank django-field

last name

password: CharField django-field

password

picture: ImageField django-field

picture

user_permissions: ManyToManyField blank django-field

user permissions: Specific permissions for this user.

username: CharField django-field

__str__(self) special

It returns readable name for the model.

Source code in users/models.py
def __str__(self: "CustomUser") -> str:
    """It returns readable name for the model."""
    return f"{self.username}"

user_upload_to(instance, filename)

A help Function to change the image upload path.

Parameters:

Name Type Description Default
instance CustomUser

django model

required
filename str

the uploaded file name

required

Returns:

Type Description
str

path in string format

Source code in users/models.py
def user_upload_to(instance: "CustomUser", filename: str) -> str:
    """A help Function to change the image upload path.

    Args:
        instance: django model
        filename: the uploaded file name

    Returns:
        path in string format
    """
    return f"images/profile_pics/{instance.username}/{filename}"

Last update: 2023-06-09
Authors: Mohamed-Kaizen