Related
I am using mongo db with djongo in django. I have two models Employee and PresentEmployee.
Employee:
class Employee(models.Model):
employee_id = models.CharField(primary_key=True, max_length=10, null=False, blank=False)
employee_name = models.CharField(max_length=200, null=False, blank=False)
email = models.EmailField(max_length=500, null=True, blank=True)
def __str__(self):
return self.employee_name
PresentEmployee:
class PresentEmployee(models.Model):
employee_id = models.OneToOneField(Employee, on_delete=models.CASCADE)
present_id = models.IntegerField(null=False, blank=False)
id = models.UUIDField(default=uuid.uuid4, primary_key=True, editable=False)
def __str__(self):
return str(self.employee_id)
when I try to add a new object to PresentEmployee, I am getting the error
web_1 | Traceback (most recent call last):
web_1 | File "/usr/local/lib/python3.10/site-packages/djongo/sql2mongo/query.py", line 808, in __iter__
web_1 | yield from iter(self._query)
web_1 | File "/usr/local/lib/python3.10/site-packages/djongo/sql2mongo/query.py", line 167, in __iter__
web_1 | yield self._align_results(doc)
web_1 | File "/usr/local/lib/python3.10/site-packages/djongo/sql2mongo/query.py", line 269, in _align_results
web_1 | if selected.table == self.left_table:
web_1 | File "/usr/local/lib/python3.10/site-packages/djongo/sql2mongo/sql_tokens.py", line 133, in table
web_1 | return alias2token[name].table
web_1 | File "/usr/local/lib/python3.10/site-packages/djongo/sql2mongo/sql_tokens.py", line 133, in table
web_1 | return alias2token[name].table
web_1 | File "/usr/local/lib/python3.10/site-packages/djongo/sql2mongo/sql_tokens.py", line 133, in table
web_1 | return alias2token[name].table
web_1 | [Previous line repeated 917 more times]
web_1 | File "/usr/local/lib/python3.10/site-packages/djongo/sql2mongo/sql_tokens.py", line 130, in table
web_1 | name = self.given_table
web_1 | File "/usr/local/lib/python3.10/site-packages/djongo/sql2mongo/sql_tokens.py", line 141, in given_table
web_1 | name = self._token.get_real_name()
web_1 | File "/usr/local/lib/python3.10/site-packages/sqlparse/sql.py", line 361, in get_real_name
web_1 | return self._get_first_name(dot_idx)
web_1 | File "/usr/local/lib/python3.10/site-packages/sqlparse/sql.py", line 386, in _get_first_name
web_1 | return token.get_name()
web_1 | File "/usr/local/lib/python3.10/site-packages/sqlparse/sql.py", line 355, in get_name
web_1 | return self.get_alias() or self.get_real_name()
web_1 | File "/usr/local/lib/python3.10/site-packages/sqlparse/sql.py", line 344, in get_alias
web_1 | _, ws = self.token_next_by(t=T.Whitespace)
web_1 | File "/usr/local/lib/python3.10/site-packages/sqlparse/sql.py", line 244, in token_next_by
web_1 | return self._token_matching(funcs, idx, end)
web_1 | File "/usr/local/lib/python3.10/site-packages/sqlparse/sql.py", line 223, in _token_matching
web_1 | if func(token):
web_1 | File "/usr/local/lib/python3.10/site-packages/sqlparse/sql.py", line 242, in <lambda>
web_1 | funcs = lambda tk: imt(tk, i, m, t)
web_1 | File "/usr/local/lib/python3.10/site-packages/sqlparse/utils.py", line 100, in imt
web_1 | elif types and any(token.ttype in ttype for ttype in types):
web_1 | File "/usr/local/lib/python3.10/site-packages/sqlparse/utils.py", line 100, in <genexpr>
web_1 | elif types and any(token.ttype in ttype for ttype in types):
web_1 | File "/usr/local/lib/python3.10/site-packages/sqlparse/tokens.py", line 19, in __contains__
web_1 | return item is not None and (self is item or item[:len(self)] == self)
web_1 | RecursionError: maximum recursion depth exceeded in comparison
web_1 |
web_1 | The above exception was the direct cause of the following exception:
web_1 |
web_1 | Traceback (most recent call last):
web_1 | File "/usr/local/lib/python3.10/site-packages/djongo/cursor.py", line 76, in fetchone
web_1 | return self.result.next()
web_1 | File "/usr/local/lib/python3.10/site-packages/djongo/sql2mongo/query.py", line 797, in __next__
web_1 | result = next(self._result_generator)
web_1 | File "/usr/local/lib/python3.10/site-packages/djongo/sql2mongo/query.py", line 830, in __iter__
web_1 | raise exe from e
web_1 | djongo.exceptions.SQLDecodeError:
web_1 |
web_1 | Keyword: FAILED SQL: SELECT %(0)s AS "a" FROM "employees_employee" WHERE "employees_employee"."employee_id" = %(1)s LIMIT 1
web_1 | Params: (1, 'EMP1')
web_1 | Version: 1.3.6
web_1 | Sub SQL: None
web_1 | FAILED SQL: None
web_1 | Params: None
web_1 | Version: None
web_1 |
web_1 | The above exception was the direct cause of the following exception:
web_1 |
web_1 | Traceback (most recent call last):
web_1 | File "/usr/local/lib/python3.10/site-packages/django/db/utils.py", line 98, in inner
web_1 | return func(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.10/site-packages/djongo/cursor.py", line 81, in fetchone
web_1 | raise db_exe from e
web_1 | djongo.database.DatabaseError
web_1 |
web_1 | The above exception was the direct cause of the following exception:
web_1 |
web_1 | Traceback (most recent call last):
web_1 | File "/usr/local/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
web_1 | response = get_response(request)
web_1 | File "/usr/local/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response
web_1 | response = wrapped_callback(request, *callback_args, **callback_kwargs)
web_1 | File "/usr/local/lib/python3.10/site-packages/django/contrib/admin/options.py", line 686, in wrapper
web_1 | return self.admin_site.admin_view(view)(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.10/site-packages/django/utils/decorators.py", line 133, in _wrapped_view
web_1 | response = view_func(request, *args, **kwargs)
web_1 | File "/usr/local/lib/python3.10/site-packages/django/views/decorators/cache.py", line 62, in _wrapped_view_func
web_1 | response = view_func(request, *args, **kwargs)
web_1 | File "/usr/local/lib/python3.10/site-packages/django/contrib/admin/sites.py", line 242, in inner
web_1 | return view(request, *args, **kwargs)
web_1 | File "/usr/local/lib/python3.10/site-packages/django/contrib/admin/options.py", line 1890, in add_view
web_1 | return self.changeform_view(request, None, form_url, extra_context)
web_1 | File "/usr/local/lib/python3.10/site-packages/django/utils/decorators.py", line 46, in _wrapper
web_1 | return bound_method(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.10/site-packages/django/utils/decorators.py", line 133, in _wrapped_view
web_1 | response = view_func(request, *args, **kwargs)
web_1 | File "/usr/local/lib/python3.10/site-packages/django/contrib/admin/options.py", line 1750, in changeform_view
web_1 | return self._changeform_view(request, object_id, form_url, extra_context)
web_1 | File "/usr/local/lib/python3.10/site-packages/django/contrib/admin/options.py", line 1796, in _changeform_view
web_1 | form_validated = form.is_valid()
web_1 | File "/usr/local/lib/python3.10/site-packages/django/forms/forms.py", line 205, in is_valid
web_1 | return self.is_bound and not self.errors
web_1 | File "/usr/local/lib/python3.10/site-packages/django/forms/forms.py", line 200, in errors
web_1 | self.full_clean()
web_1 | File "/usr/local/lib/python3.10/site-packages/django/forms/forms.py", line 439, in full_clean
web_1 | self._post_clean()
web_1 | File "/usr/local/lib/python3.10/site-packages/django/forms/models.py", line 492, in _post_clean
web_1 | self.instance.full_clean(exclude=exclude, validate_unique=False)
web_1 | File "/usr/local/lib/python3.10/site-packages/django/db/models/base.py", line 1464, in full_clean
web_1 | self.clean_fields(exclude=exclude)
web_1 | File "/usr/local/lib/python3.10/site-packages/django/db/models/base.py", line 1516, in clean_fields
web_1 | setattr(self, f.attname, f.clean(raw_value, self))
web_1 | File "/usr/local/lib/python3.10/site-packages/django/db/models/fields/__init__.py", line 755, in clean
web_1 | self.validate(value, model_instance)
web_1 | File "/usr/local/lib/python3.10/site-packages/django/db/models/fields/related.py", line 1090, in validate
web_1 | if not qs.exists():
web_1 | File "/usr/local/lib/python3.10/site-packages/django/db/models/query.py", line 1225, in exists
web_1 | return self.query.has_results(using=self.db)
web_1 | File "/usr/local/lib/python3.10/site-packages/django/db/models/sql/query.py", line 592, in has_results
web_1 | return compiler.has_results()
web_1 | File "/usr/local/lib/python3.10/site-packages/django/db/models/sql/compiler.py", line 1363, in has_results
web_1 | return bool(self.execute_sql(SINGLE))
web_1 | File "/usr/local/lib/python3.10/site-packages/django/db/models/sql/compiler.py", line 1406, in execute_sql
web_1 | val = cursor.fetchone()
web_1 | File "/usr/local/lib/python3.10/site-packages/django/db/utils.py", line 97, in inner
web_1 | with self:
web_1 | File "/usr/local/lib/python3.10/site-packages/django/db/utils.py", line 91, in __exit__
web_1 | raise dj_exc_value.with_traceback(traceback) from exc_value
web_1 | File "/usr/local/lib/python3.10/site-packages/django/db/utils.py", line 98, in inner
web_1 | return func(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.10/site-packages/djongo/cursor.py", line 81, in fetchone
web_1 | raise db_exe from e
web_1 | django.db.utils.DatabaseError
How can I establish a One to One relation from Employee model to PresentEmployee model. Like a OneToOneField in sql db like postgresql.
note: never mind the web_1 |, it is because I am using docker.
If you are using djongo, you can create EmbeddedField class:
class EmbeddedField(MongoField):
def __init__(
self,
model_container: typing.Type[Model],
model_form_class: typing.Type[forms.ModelForm] = None,
model_form_kwargs: dict = None,
*args,
**kwargs
):
After that, with your example you can create something like this
class Employee(models.Model):
employee_id = models.ObjectIdField()
employee_name = models.CharField(max_length=200, null=False, blank=False)
email = models.EmailField(max_length=500, null=True, blank=True)
def __str__(self):
return self.employee_name
class PresentEmployee(models.Model):
present_id = models.ObjectIdField()
employee = models.EmbeddedField(model_container=Employee)
id = models.UUIDField(default=uuid.uuid4, primary_key=True, editable=False)
def __str__(self):
return str(self.employee_id)
I am trying to deploy to AWS(EC2) a Cookiecutter Django project.
The AWS user with this credentials has ful S3, SES and SNS policies.
The EC2 server has also a role with full SES/S3 policies.
In production file in envs I have the keys set up like this.
DJANGO_AWS_ACCESS_KEY_ID=xxxxxxxxx
DJANGO_AWS_SECRET_ACCESS_KEY=xxxxxxxxxx
DJANGO_AWS_STORAGE_BUCKET_NAME=xxxxxxxxxx
In settings I have
AWS_S3_REGION_NAME = env("DJANGO_AWS_S3_REGION_NAME", default=None)
AWS_ACCESS_KEY_ID = env("DJANGO_AWS_ACCESS_KEY_ID")
AWS_SECRET_ACCESS_KEY = env("DJANGO_AWS_SECRET_ACCESS_KEY")
AWS_STORAGE_BUCKET_NAME = env("DJANGO_AWS_STORAGE_BUCKET_NAME")
EMAIL_BACKEND = "anymail.backends.amazon_ses.EmailBackend"
ANYMAIL = {}
All nice and fine until the project tries to send an email using SES and it crashes with the error bellow .
Until now I have tried:
adding DJANGO_AWS_S3_REGION_NAME to the production file in envs - no result
adding the region in aws config using aws cli - no result
overriding the settings in ANYMAIL ={} with credetials and region - no result
making a blank project, just adding the aws credentials and not changing anything else - no result
creating manually on another project a boto3.session.client with the same credentials and sending a mail - it works
This is the error. The second part with 'NoneType' object has no attribute 'send_raw_email' repeats a lot after this.
django_1 | [2021-08-13 13:58:14 +0000] [12] [ERROR] Error handling request /accounts/signup/
django_1 | Traceback (most recent call last):
django_1 | File "/usr/local/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
django_1 | response = get_response(request)
django_1 | File "/usr/local/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response
django_1 | response = wrapped_callback(request, *callback_args, **callback_kwargs)
django_1 | File "/usr/local/lib/python3.9/contextlib.py", line 79, in inner
django_1 | return func(*args, **kwds)
django_1 | File "/usr/local/lib/python3.9/site-packages/django/views/generic/base.py", line 70, in view
django_1 | return self.dispatch(request, *args, **kwargs)
django_1 | File "/usr/local/lib/python3.9/site-packages/django/utils/decorators.py", line 43, in _wrapper
django_1 | return bound_method(*args, **kwargs)
django_1 | File "/usr/local/lib/python3.9/site-packages/django/views/decorators/debug.py", line 89, in sensitive_post_parameters_wrapper
django_1 | return view(request, *args, **kwargs)
django_1 | File "/usr/local/lib/python3.9/site-packages/allauth/account/views.py", line 230, in dispatch
django_1 | return super(SignupView, self).dispatch(request, *args, **kwargs)
django_1 | File "/usr/local/lib/python3.9/site-packages/allauth/account/views.py", line 74, in dispatch
django_1 | response = super(RedirectAuthenticatedUserMixin, self).dispatch(
django_1 | File "/usr/local/lib/python3.9/site-packages/allauth/account/views.py", line 204, in dispatch
django_1 | return super(CloseableSignupMixin, self).dispatch(request, *args, **kwargs)
django_1 | File "/usr/local/lib/python3.9/site-packages/django/views/generic/base.py", line 98, in dispatch
django_1 | return handler(request, *args, **kwargs)
django_1 | File "/usr/local/lib/python3.9/site-packages/allauth/account/views.py", line 102, in post
django_1 | response = self.form_valid(form)
django_1 | File "/usr/local/lib/python3.9/site-packages/allauth/account/views.py", line 248, in form_valid
django_1 | return complete_signup(
django_1 | File "/usr/local/lib/python3.9/site-packages/allauth/account/utils.py", line 209, in complete_signup
django_1 | return perform_login(
django_1 | File "/usr/local/lib/python3.9/site-packages/allauth/account/utils.py", line 175, in perform_login
django_1 | send_email_confirmation(request, user, signup=signup, email=email)
django_1 | File "/usr/local/lib/python3.9/site-packages/allauth/account/utils.py", line 346, in send_email_confirmation
django_1 | email_address.send_confirmation(request, signup=signup)
django_1 | File "/usr/local/lib/python3.9/site-packages/allauth/account/models.py", line 62, in send_confirmation
django_1 | confirmation.send(request, signup=signup)
django_1 | File "/usr/local/lib/python3.9/site-packages/allauth/account/models.py", line 169, in send
django_1 | get_adapter(request).send_confirmation_mail(request, self, signup)
django_1 | File "/usr/local/lib/python3.9/site-packages/allauth/account/adapter.py", line 464, in send_confirmation_mail
django_1 | self.send_mail(email_template, emailconfirmation.email_address.email, ctx)
django_1 | File "/usr/local/lib/python3.9/site-packages/allauth/account/adapter.py", line 136, in send_mail
django_1 | msg.send()
django_1 | File "/usr/local/lib/python3.9/site-packages/django/core/mail/message.py", line 284, in send
django_1 | return self.get_connection(fail_silently).send_messages([self])
django_1 | File "/usr/local/lib/python3.9/site-packages/anymail/backends/base.py", line 89, in send_messages
django_1 | created_session = self.open()
django_1 | File "/usr/local/lib/python3.9/site-packages/anymail/backends/amazon_ses.py", line 44, in open
django_1 | self.client = boto3.session.Session(**self.session_params).client("ses", **self.client_params)
django_1 | File "/usr/local/lib/python3.9/site-packages/boto3/session.py", line 258, in client
django_1 | return self._session.create_client(
django_1 | File "/usr/local/lib/python3.9/site-packages/botocore/session.py", line 847, in create_client
django_1 | client = client_creator.create_client(
django_1 | File "/usr/local/lib/python3.9/site-packages/botocore/client.py", line 86, in create_client
django_1 | client_args = self._get_client_args(
django_1 | File "/usr/local/lib/python3.9/site-packages/botocore/client.py", line 355, in _get_client_args
django_1 | return args_creator.get_client_args(
django_1 | File "/usr/local/lib/python3.9/site-packages/botocore/args.py", line 71, in get_client_args
django_1 | final_args = self.compute_client_args(
django_1 | File "/usr/local/lib/python3.9/site-packages/botocore/args.py", line 148, in compute_client_args
django_1 | endpoint_config = self._compute_endpoint_config(
django_1 | File "/usr/local/lib/python3.9/site-packages/botocore/args.py", line 220, in _compute_endpoint_config
django_1 | return self._resolve_endpoint(**resolve_endpoint_kwargs)
django_1 | File "/usr/local/lib/python3.9/site-packages/botocore/args.py", line 302, in _resolve_endpoint
django_1 | return endpoint_bridge.resolve(
django_1 | File "/usr/local/lib/python3.9/site-packages/botocore/client.py", line 430, in resolve
django_1 | resolved = self.endpoint_resolver.construct_endpoint(
django_1 | File "/usr/local/lib/python3.9/site-packages/botocore/regions.py", line 133, in construct_endpoint
django_1 | result = self._endpoint_for_partition(
django_1 | File "/usr/local/lib/python3.9/site-packages/botocore/regions.py", line 148, in _endpoint_for_partition
django_1 | raise NoRegionError()
django_1 | botocore.exceptions.NoRegionError: You must specify a region.
django_1 |
django_1 | During handling of the above exception, another exception occurred:
django_1 |
django_1 | Traceback (most recent call last):
django_1 | File "/usr/local/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
django_1 | response = get_response(request)
django_1 | File "/usr/local/lib/python3.9/site-packages/django/utils/deprecation.py", line 114, in __call__
django_1 | response = response or self.get_response(request)
django_1 | File "/usr/local/lib/python3.9/site-packages/django/core/handlers/exception.py", line 49, in inner
django_1 | response = response_for_exception(request, exc)
django_1 | File "/usr/local/lib/python3.9/site-packages/django/core/handlers/exception.py", line 104, in response_for_exception
django_1 | log_response(
django_1 | File "/usr/local/lib/python3.9/site-packages/django/utils/log.py", line 224, in log_response
django_1 | getattr(logger, level)(
django_1 | File "/usr/local/lib/python3.9/logging/__init__.py", line 1475, in error
django_1 | self._log(ERROR, msg, args, **kwargs)
django_1 | File "/usr/local/lib/python3.9/logging/__init__.py", line 1589, in _log
django_1 | self.handle(record)
django_1 | File "/usr/local/lib/python3.9/logging/__init__.py", line 1599, in handle
django_1 | self.callHandlers(record)
django_1 | File "/usr/local/lib/python3.9/logging/__init__.py", line 1661, in callHandlers
django_1 | hdlr.handle(record)
django_1 | File "/usr/local/lib/python3.9/logging/__init__.py", line 952, in handle
django_1 | self.emit(record)
django_1 | File "/usr/local/lib/python3.9/site-packages/django/utils/log.py", line 122, in emit
django_1 | self.send_mail(subject, message, fail_silently=True, html_message=html_message)
django_1 | File "/usr/local/lib/python3.9/site-packages/django/utils/log.py", line 125, in send_mail
django_1 | mail.mail_admins(subject, message, *args, connection=self.connection(), **kwargs)
django_1 | File "/usr/local/lib/python3.9/site-packages/django/core/mail/__init__.py", line 104, in mail_admins
django_1 | mail.send(fail_silently=fail_silently)
django_1 | File "/usr/local/lib/python3.9/site-packages/django/core/mail/message.py", line 284, in send
django_1 | return self.get_connection(fail_silently).send_messages([self])
django_1 | File "/usr/local/lib/python3.9/site-packages/anymail/backends/base.py", line 94, in send_messages
django_1 | sent = self._send(message)
django_1 | File "/usr/local/lib/python3.9/site-packages/anymail/backends/base.py", line 124, in _send
django_1 | response = self.post_to_esp(payload, message)
django_1 | File "/usr/local/lib/python3.9/site-packages/anymail/backends/amazon_ses.py", line 67, in post_to_esp
django_1 | response = payload.call_send_api(self.client)
django_1 | File "/usr/local/lib/python3.9/site-packages/anymail/backends/amazon_ses.py", line 127, in call_send_api
django_1 | return ses_client.send_raw_email(**self.params)
django_1 | AttributeError: 'NoneType' object has no attribute 'send_raw_email'
I'll appreciate any input. I'm out of ideas.
Thanks!
As you've probably figured out, the problem is boto3 doesn't know what AWS region you're trying to operate in:
botocore.exceptions.NoRegionError: You must specify a region.
The region name comes from boto3 configuration. From Anymail's SES docs:
you must make sure boto3 is configured with AWS credentials having the necessary IAM permissions. There are several ways to do this; see Credentials in the Boto docs for options. Usually, an IAM role for EC2 instances, standard Boto environment variables, or a shared AWS credentials file will be appropriate. For more complex cases, use Anymail’s AMAZON_SES_CLIENT_PARAMS setting to customize the Boto session.
It seems like you might be try to mix some of the "several ways" to provide boto3 credentials, which could be causing confusion.
Note that your AWS_* Django settings don't come into play. boto3 doesn't know about Django settings. Anymail's SES settings docs describe which Django settings Anymail will pass along to boto3. (And AWS_S3_REGION_NAME wouldn't be relevant anyway, because S3 is not the same service as SES. I'm guessing those AWS_* Django settings may be for some other app, maybe django-storages.)
If you want to provide AWS credentials specifically for Anymail in settings.py, you can do that with Anymail's AMAZON_SES_CLIENT_PARAMS setting. E.g.:
# (Be sure to add DJANGO_AWS_REGION_NAME to your env to use this example)
ANYMAIL = {
"AMAZON_SES_CLIENT_PARAMS": {
"aws_access_key_id": env("DJANGO_AWS_ACCESS_KEY_ID"),
"aws_secret_access_key": env("DJANGO_AWS_SECRET_ACCESS_KEY"),
"region_name": env("DJANGO_AWS_REGION_NAME"),
},
}
I have a Django application running on docker connected to a database in another container on the same host. This seams to work fine, but when I try to change the connection to a database on another server, it fails to connect. Not only that, but when I try to connect to the Django application in the browser(like admin or api) I get no response, and see no activity in the output log. The application runs fine with the remote database if I run it outside the container, and the database is set to accept all IPs for the user I am trying to connect with.
Any Ideas as to why I am not getting a connection?
Dockerfile:
FROM python:3.8-alpine
ENV PATH="/scripts:${PATH}"
COPY ./requirements.txt /requirements.txt
RUN apk add --update --no-cache --virtual .tmp gcc libc-dev linux-headers
RUN apk add mariadb-dev python3-dev postgresql-dev
RUN pip install -r /requirements.txt
RUN apk del .tmp
RUN mkdir /app_django
COPY ./app_django /app_django
WORKDIR /app_django
COPY ./scripts /scripts
RUN chmod +x /scripts/*
RUN mkdir -p /vol/web/media
RUN mkdir -p /vol/web/static
RUN adduser -D user
RUN chown -R user:user /vol
RUN chmod -R 755 /vol/web
USER user
CMD ["entrypoint.sh"]
docker-compose.yml
version: '3.7'
services:
db:
build: mysql/
environment:
MYSQL_ROOT_PASSWORD: 'password'
MYSQL_DATABASE: database
volumes:
- ./mysql/data:/var/lib/mysql
- ./mysql/conf:/etc/mysql/conf.d
- ./mysql/entrypoint-initdb.d:/docker-entrypoint-initdb.d/
- ./mysql/backup_database:/var/local/mysql/backups
ports:
- 3306:3306
restart: always
pdb:
build:
context: .
ports:
- "8000:8000"
volumes:
- ./pdb_django:/pdb_django
command: sh -c "python manage.py runserver 0.0.0.0:8000"
environment:
- DEBUG=1
entrypoint.sh
#!/bin/sh
set -e
python manage.py collectstatic --noinput
uwsgi --socket :8000 --master --enable-threads --module app.wsgi
Django db settings (the commented parts is used when conection to db on host container):
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'database',
'HOST': '<remote IP>',
# 'HOST': 'db',
'USER': 'user',
'PASSWORD': 'password',
'PORT': '3308'
# 'PORT': '3306',
}
}
requirements.txt
Django==3.1.4
uWSGI>=2.0.18,<2.1
django-filter==2.4.0
djangorestframework==3.12.2
mysql-connector-python==8.0.22
mysqlclient==2.0.2
PyMySQL==0.10.1
sqlparse==0.4.1
Outputlog for application:
pdb_1 | Watching for file changes with StatReloader
pdb_1 | Exception in thread django-main-thread:
pdb_1 | Traceback (most recent call last):
pdb_1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
pdb_1 | self.connect()
pdb_1 | File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
pdb_1 | return func(*args, **kwargs)
pdb_1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 200, in connect
pdb_1 | self.connection = self.get_new_connection(conn_params)
pdb_1 | File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
pdb_1 | return func(*args, **kwargs)
pdb_1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/mysql/base.py", line 234, in get_new_connection
pdb_1 | return Database.connect(**conn_params)
pdb_1 | File "/usr/local/lib/python3.8/site-packages/MySQLdb/__init__.py", line 130, in Connect
pdb_1 | return Connection(*args, **kwargs)
pdb_1 | File "/usr/local/lib/python3.8/site-packages/MySQLdb/connections.py", line 185, in __init__
pdb_1 | super().__init__(*args, **kwargs2)
pdb_1 | MySQLdb._exceptions.OperationalError: (2002, "Can't connect to MySQL server on '<remote IP>' (115)")
pdb_1 |
pdb_1 | The above exception was the direct cause of the following exception:
pdb_1 |
pdb_1 | Traceback (most recent call last):
pdb_1 | File "/usr/local/lib/python3.8/threading.py", line 932, in _bootstrap_inner
pdb_1 | self.run()
pdb_1 | File "/usr/local/lib/python3.8/threading.py", line 870, in run
pdb_1 | self._target(*self._args, **self._kwargs)
pdb_1 | File "/usr/local/lib/python3.8/site-packages/django/utils/autoreload.py", line 53, in wrapper
pdb_1 | fn(*args, **kwargs)
pdb_1 | File "/usr/local/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 121, in inner_run
pdb_1 | self.check_migrations()
pdb_1 | File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 459, in check_migrations
pdb_1 | executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
pdb_1 | File "/usr/local/lib/python3.8/site-packages/django/db/migrations/executor.py", line 18, in __init__
pdb_1 | self.loader = MigrationLoader(self.connection)
pdb_1 | File "/usr/local/lib/python3.8/site-packages/django/db/migrations/loader.py", line 53, in __init__
pdb_1 | self.build_graph()
pdb_1 | File "/usr/local/lib/python3.8/site-packages/django/db/migrations/loader.py", line 216, in build_graph
pdb_1 | self.applied_migrations = recorder.applied_migrations()
pdb_1 | File "/usr/local/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 77, in applied_migrations
pdb_1 | if self.has_table():
pdb_1 | File "/usr/local/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 55, in has_table
pdb_1 | with self.connection.cursor() as cursor:
pdb_1 | File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
pdb_1 | return func(*args, **kwargs)
pdb_1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 259, in cursor
pdb_1 | return self._cursor()
pdb_1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 235, in _cursor
pdb_1 | self.ensure_connection()
pdb_1 | File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
pdb_1 | return func(*args, **kwargs)
pdb_1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
pdb_1 | self.connect()
pdb_1 | File "/usr/local/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
pdb_1 | raise dj_exc_value.with_traceback(traceback) from exc_value
pdb_1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
pdb_1 | self.connect()
pdb_1 | File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
pdb_1 | return func(*args, **kwargs)
pdb_1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 200, in connect
pdb_1 | self.connection = self.get_new_connection(conn_params)
pdb_1 | File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
pdb_1 | return func(*args, **kwargs)
pdb_1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/mysql/base.py", line 234, in get_new_connection
pdb_1 | return Database.connect(**conn_params)
pdb_1 | File "/usr/local/lib/python3.8/site-packages/MySQLdb/__init__.py", line 130, in Connect
pdb_1 | return Connection(*args, **kwargs)
pdb_1 | File "/usr/local/lib/python3.8/site-packages/MySQLdb/connections.py", line 185, in __init__
pdb_1 | super().__init__(*args, **kwargs2)
pdb_1 | django.db.utils.OperationalError: (2002, "Can't connect to MySQL server on '<remote IP>' (115)")
It would seem this was an issue with my VPN. When I tried to run the container on a server on the network it worked fine, and I was also able to get responses when I turned off my VPN (although I could obviously not access the database on the server)
I've been struggling to get a simple app running using Django, Djongo, Mongo, and Docker Compose. My setup looks like this:
docker-compose.yml
services:
mongodb:
image: mongo:latest
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: mongoadmin
MONGO_INITDB_DATABASE: django_mongodb_docker
ports:
- 27017:27017
web:
build: ./src
restart: always
command: python src/manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- 8000:8000
links:
- mongodb
Dockerfile
FROM python:3
ENV PYTHONUNBUFFERED=1
RUN mkdir /code
COPY . /code/
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
settings.py
DATABASES = {
'default': {
'ENGINE': 'djongo',
'HOST': 'mongodb',
'PORT': 27017,
'USER': 'root',
'PASSWORD': 'mongoadmin',
'AUTH_SOURCE': 'admin',
'AUTH_MECHANISM': 'SCRAM-SHA-1',
}
}
What is annoying is that I am able to use pymongo from my web container to connect to the container running mongo. That works as follows.
from pymongo import MongoClient
c = MongoClient(
'mongodb://mongodb:27017',
username='root',
password='mongoadmin',
authSource='admin',
authMechanism='SCRAM-SHA-1')
print(c.server_info())
The issue is that when I go to run migrations from within my web container I get the following error.
Traceback (most recent call last):
File "/code/src/manage.py", line 22, in <module>
main()
File "/code/src/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 371, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 85, in wrapped
res = handle_func(*args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/django/core/management/commands/migrate.py", line 92, in handle
executor = MigrationExecutor(connection, self.migration_progress_callback)
File "/usr/local/lib/python3.9/site-packages/django/db/migrations/executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
File "/usr/local/lib/python3.9/site-packages/django/db/migrations/loader.py", line 53, in __init__
self.build_graph()
File "/usr/local/lib/python3.9/site-packages/django/db/migrations/loader.py", line 216, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "/usr/local/lib/python3.9/site-packages/django/db/migrations/recorder.py", line 77, in applied_migrations
if self.has_table():
File "/usr/local/lib/python3.9/site-packages/django/db/migrations/recorder.py", line 56, in has_table
tables = self.connection.introspection.table_names(cursor)
File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/introspection.py", line 48, in table_names
return get_names(cursor)
File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/introspection.py", line 43, in get_names
return sorted(ti.name for ti in self.get_table_list(cursor)
File "/usr/local/lib/python3.9/site-packages/djongo/introspection.py", line 47, in get_table_list
for c in cursor.db_conn.list_collection_names()
File "/usr/local/lib/python3.9/site-packages/pymongo/database.py", line 863, in list_collection_names
for result in self.list_collections(session=session, **kwargs)]
File "/usr/local/lib/python3.9/site-packages/pymongo/database.py", line 825, in list_collections
return self.__client._retryable_read(
File "/usr/local/lib/python3.9/site-packages/pymongo/mongo_client.py", line 1460, in _retryable_read
server = self._select_server(
File "/usr/local/lib/python3.9/site-packages/pymongo/mongo_client.py", line 1278, in _select_server
server = topology.select_server(server_selector)
File "/usr/local/lib/python3.9/site-packages/pymongo/topology.py", line 241, in select_server
return random.choice(self.select_servers(selector,
File "/usr/local/lib/python3.9/site-packages/pymongo/topology.py", line 199, in select_servers
server_descriptions = self._select_servers_loop(
File "/usr/local/lib/python3.9/site-packages/pymongo/topology.py", line 215, in _select_servers_loop
raise ServerSelectionTimeoutError(
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused, Timeout: 30s, Topology Description: <TopologyDescription id: 5f9ecaa5bbdc0433baa13966, topology_type: Single, servers: [<ServerDescription ('localhost', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('localhost:27017: [Errno 111] Connection refused')>]>
In addition, I've tried to create a Djongo model and save it to see if maybe the problem is specific to migrations (essentially just trying to make any connection to mongo using djongo).
models.py
from djongo import models
class Blog(models.Model):
name = models.CharField(max_length=100)
test.py
b = Blog(name='test')
b.save()
That returns the following error:
The above exception was the direct cause of the following exception:
web_1 |
web_1 | Traceback (most recent call last):
web_1 | File "/usr/local/lib/python3.9/site-packages/djongo/cursor.py", line 51, in execute
web_1 | self.result = Query(
web_1 | File "/usr/local/lib/python3.9/site-packages/djongo/sql2mongo/query.py", line 783, in __init__
web_1 | self._query = self.parse()
web_1 | File "/usr/local/lib/python3.9/site-packages/djongo/sql2mongo/query.py", line 884, in parse
web_1 | raise exe from e
web_1 | djongo.exceptions.SQLDecodeError:
web_1 |
web_1 | Keyword: None
web_1 | Sub SQL: None
web_1 | FAILED SQL: INSERT INTO "game_blog" ("name") VALUES (%(0)s)
web_1 | Params: ['test']
web_1 | Version: 1.3.3
web_1 |
web_1 | The above exception was the direct cause of the following exception:
web_1 |
web_1 | Traceback (most recent call last):
web_1 | File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
web_1 | return self.cursor.execute(sql, params)
web_1 | File "/usr/local/lib/python3.9/site-packages/djongo/cursor.py", line 59, in execute
web_1 | raise db_exe from e
web_1 | djongo.database.DatabaseError
web_1 |
web_1 | The above exception was the direct cause of the following exception:
web_1 |
web_1 | Traceback (most recent call last):
web_1 | File "/usr/local/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
web_1 | response = get_response(request)
web_1 | File "/usr/local/lib/python3.9/site-packages/django/core/handlers/base.py", line 179, in _get_response
web_1 | response = wrapped_callback(request, *callback_args, **callback_kwargs)
web_1 | File "/usr/local/lib/python3.9/site-packages/django/views/generic/base.py", line 70, in view
web_1 | return self.dispatch(request, *args, **kwargs)
web_1 | File "/usr/local/lib/python3.9/site-packages/django/views/generic/base.py", line 98, in dispatch
web_1 | return handler(request, *args, **kwargs)
web_1 | File "/code/src/game/views.py", line 9, in get
web_1 | b.save(using='mongo')
web_1 | File "/usr/local/lib/python3.9/site-packages/django/db/models/base.py", line 753, in save
web_1 | self.save_base(using=using, force_insert=force_insert,
web_1 | File "/usr/local/lib/python3.9/site-packages/django/db/models/base.py", line 790, in save_base
web_1 | updated = self._save_table(
web_1 | File "/usr/local/lib/python3.9/site-packages/django/db/models/base.py", line 895, in _save_table
web_1 | results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
web_1 | File "/usr/local/lib/python3.9/site-packages/django/db/models/base.py", line 933, in _do_insert
web_1 | return manager._insert(
web_1 | File "/usr/local/lib/python3.9/site-packages/django/db/models/manager.py", line 85, in manager_method
web_1 | return getattr(self.get_queryset(), name)(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.9/site-packages/django/db/models/query.py", line 1254, in _insert
web_1 | return query.get_compiler(using=using).execute_sql(returning_fields)
web_1 | File "/usr/local/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1397, in execute_sql
web_1 | cursor.execute(sql, params)
web_1 | File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 98, in execute
web_1 | return super().execute(sql, params)
web_1 | File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 66, in execute
web_1 | return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
web_1 | File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
web_1 | return executor(sql, params, many, context)
web_1 | File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
web_1 | return self.cursor.execute(sql, params)
web_1 | File "/usr/local/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__
web_1 | raise dj_exc_value.with_traceback(traceback) from exc_value
web_1 | File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
web_1 | return self.cursor.execute(sql, params)
web_1 | File "/usr/local/lib/python3.9/site-packages/djongo/cursor.py", line 59, in execute
web_1 | raise db_exe from e
web_1 | django.db.utils.DatabaseError
Any help on this would be greatly appreciated.
I have tried editing my mongodb.conf like this already.
According to this document, the settings.py should have a CLIENT section which contains:
A set of key-value pairs that will be passed directly to MongoClient as kwargs while creating a new client connection.
So try setting your settings.py to:
DATABASE = {
'default': {
'ENGINE': 'djongo',
'NAME': 'your-database-name',
'CLIENT': {
'host': 'mongodb://mongodb:27017',
'username': 'root',
'password': 'mongoadmin',
'authSource': 'admin',
'authMechanism': 'SCRAM-SHA-1',
}
}
}
I've a fairly simple django-channels/daphne/asgi/redis app. The app never has any problem when using chrome but fails miserably when we use firefox (latest version ~ 60). I currently have the following error:
api_1 | 2018-06-12 16:02:24,378 INFO In the connect method
api_1 | xxxxxx:41647 - - [12/Jun/2018:16:02:24] "WSCONNECT /updates/" - -
api_1 | 2018-06-12 16:02:25,049 ERROR Exception inside application: [Errno -2] Name or service not known
api_1 | File "/usr/local/lib/python3.6/site-packages/channels/consumer.py", line 54, in __call__
api_1 | await await_many_dispatch([receive, self.channel_receive], self.dispatch)
api_1 | File "/usr/local/lib/python3.6/site-packages/channels/utils.py", line 48, in await_many_dispatch
api_1 | await dispatch(result)
api_1 | File "/usr/local/lib/python3.6/site-packages/asgiref/sync.py", line 110, in __call__
api_1 | return await asyncio.wait_for(future, timeout=None)
api_1 | File "/usr/local/lib/python3.6/asyncio/tasks.py", line 339, in wait_for
api_1 | return (yield from fut)
api_1 | File "/usr/local/lib/python3.6/concurrent/futures/thread.py", line 56, in run
api_1 | result = self.fn(*self.args, **self.kwargs)
api_1 | File "/usr/local/lib/python3.6/site-packages/channels/db.py", line 13, in thread_handler
api_1 | return super().thread_handler(loop, *args, **kwargs)
api_1 | File "/usr/local/lib/python3.6/site-packages/asgiref/sync.py", line 125, in thread_handler
api_1 | return self.func(*args, **kwargs)
api_1 | File "/usr/local/lib/python3.6/site-packages/channels/consumer.py", line 99, in dispatch
api_1 | handler(message)
api_1 | File "/usr/local/lib/python3.6/site-packages/channels/generic/websocket.py", line 19, in websocket_connect
api_1 | self.connect()
api_1 | File "./flypoll/consumers.py", line 146, in connect
api_1 | async_to_sync(self.channel_layer.group_add)("flypoll_socket_clients", self.channel_name)
api_1 | File "/usr/local/lib/python3.6/site-packages/asgiref/sync.py", line 64, in __call__
api_1 | return call_result.result()
api_1 | File "/usr/local/lib/python3.6/concurrent/futures/_base.py", line 432, in result
api_1 | return self.__get_result()
api_1 | File "/usr/local/lib/python3.6/concurrent/futures/_base.py", line 384, in __get_result
api_1 | raise self._exception
api_1 | File "/usr/local/lib/python3.6/site-packages/asgiref/sync.py", line 78, in main_wrap
api_1 | result = await self.awaitable(*args, **kwargs)
api_1 | File "/usr/local/lib/python3.6/site-packages/channels_redis/core.py", line 283, in group_add
api_1 | async with self.connection(self.consistent_hash(group)) as connection:
api_1 | File "/usr/local/lib/python3.6/site-packages/channels_redis/core.py", line 403, in __aenter__
api_1 | self.conn = await aioredis.create_redis(**self.kwargs)
api_1 | File "/usr/local/lib/python3.6/site-packages/aioredis/commands/__init__.py", line 174, in create_redis
api_1 | loop=loop)
api_1 | File "/usr/local/lib/python3.6/site-packages/aioredis/connection.py", line 107, in create_connection
api_1 | timeout, loop=loop)
api_1 | File "/usr/local/lib/python3.6/asyncio/tasks.py", line 339, in wait_for
api_1 | return (yield from fut)
api_1 | File "/usr/local/lib/python3.6/site-packages/aioredis/stream.py", line 19, in open_connection
api_1 | lambda: protocol, host, port, **kwds)
api_1 | File "/usr/local/lib/python3.6/asyncio/base_events.py", line 734, in create_connection
api_1 | infos = f1.result()
api_1 | File "/usr/local/lib/python3.6/concurrent/futures/thread.py", line 56, in run
api_1 | result = self.fn(*self.args, **self.kwargs)
api_1 | File "/usr/local/lib/python3.6/socket.py", line 745, in getaddrinfo
api_1 | for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
api_1 | [Errno -2] Name or service not known
It says there is a problem with connect method but the connect method is as follows:
def connect(self):
"""
standard connect handler, accepts the connection, and sets up some basic parameters
:return:
"""
logging.info("In the connect method")
self.accept()
async_to_sync(self.channel_layer.group_add)("flypoll_socket_clients", self.channel_name)
The app is dockerized and is hosted on ec2. The redis, python, django are dockerized too. I've updated the modules (asgiref, txaio, twisted, channels, daphne etc) to latest versions but i'm not able to get rid of this.
What could be wrong?
It looks like your django container can't access redis.
In your django settings you must have something like this :
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [('[redis-host-here]', 6379)], # REPLACE redis host here
},
},
}
And that host must be accessible from your django container