from django.contrib.auth.base_user import BaseUserManager
from django.utils.translation import ugettext_lazy as _
class CustomUserManager(BaseUserManager):
"""
Custom user model manager where email is the unique identifiers
for authentication instead of usernames.
"""
def create_user(self, email, password, **extra_fields):
"""
Create and save a User with the given email and password.
"""
if not email:
raise ValueError(_('The Email must be set'))
email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save()
return user
def create_superuser(self, email, password, **extra_fields):
"""
Create and save a SuperUser with the given email and password.
"""
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
extra_fields.setdefault('is_active', True)
if extra_fields.get('is_staff') is not True:
raise ValueError(_('Superuser must have is_staff=True.'))
if extra_fields.get('is_superuser') is not True:
raise ValueError(_('Superuser must have is_superuser=True.'))
return self.create_user(email, password, **extra_fields)
error message:
File "C:\Users\zesha\Documents\GitHub\sust-cse-alumni-repo\users\models.py", line 6, in <module>
from .managers import CustomUserManager
File "C:\Users\zesha\Documents\GitHub\sust-cse-alumni-repo\users\managers.py", line 2, in <module>
from django.utils.translation import ugettext_lazy as _
ImportError: cannot import name 'ugettext_lazy' from 'django.utils.translation' (C:\Users\zesha\Documents\GitHub\sust-cse-al
umni-repo\iftu\lib\site-packages\django\utils\translation\__init__.py)
Hi NobinPegasus, ugettext_lazy` was deprecated in v2.2 and no longer used in django v3+. Check your version of django’s documentation regarding lazy translation (here’s v3.2).
the issue. Currently on each new Django 4.0.2 installation we need to:
open: /opt/homebrew/lib/python3.9/site-packages/jsonfield/fields.py
change the line 9 from:
from django.utils.translation import ugettext_lazy as _
to:
from django.utils.translation import gettext_lazy as _
What is this package “jsonfield”?
From its path, it looks like it’s a third party package and not part of Django - in which case the issue needs to be addressed by that third party library and is not a Django issue.
Where do you find the location of the folder “site-packages”?
I looked at the directory for the file
File "/home/eric/.local/lib/python3.8/site-packages/django_celery_monitor/apps.py", line 5, in <module>
from django.utils.translation import ugettext_lazy as _
and wasn’t sure where to locate the file apps.py. Would you like to provide a tutorial on where to locate the file?
I am using Wagtail 6.0.1 and Django 5.0.3. The file I am using has this code has this code from django.utils.translation import gettext_lazy as _
However, I am still getting this error:
from django.utils.translation import ugettext_lazy as _
ImportError: cannot import name 'ugettext_lazy' from 'django.utils.translation' (/venv/lib/python3.12/site-packages/django/utils/translation/__init__.py).
the venv is generated from the docker image
FROM python:3.12.2-slim as builder-py
ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1
WORKDIR /code
ENV VIRTUAL_ENV=/venv
RUN python3 -m venv $VIRTUAL_ENV
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
RUN apt update && apt install -y build-essential gcc zlib1g-dev libjpeg-dev && apt clean
RUN pip install -U pip
ADD ./requirements.txt /code/
RUN --mount=type=cache,target=/root/.cache pip install --no-compile -r /code/requirements.txt
FROM python:3.12.2-slim
ENV VIRTUAL_ENV=/venv
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1
COPY --from=builder-py /venv /venv
COPY --from=builder-py /code /code
RUN apt update && apt install -y git && apt clean
WORKDIR /code
EXPOSE 8000
COPY . /code/
CMD ["gunicorn", "-c", "/code/gunicorn.conf.py", "config.wsgi"]
LABEL VERSION v9.4.20
LABEL Description="Image for www.velda.com" Vendor=“Velda” Version="9.4.20"
ENV VERSION v9.4.20
ENV PYTHONPATH /code:$PYTHONPATH
This issue occurs because the ugettext_lazy alias was deprecated in Django 2.0 and completely removed in Django 3.0. This is probably a third-party app you are using trying to import ‘ugettext_lazy’
To solve that, put the following code in your settings.py file
# settings.py
import django.utils.translation as original_translation
from django.utils.translation import gettext_lazy
original_translation.ugettext_lazy = gettext_lazy