TypeError: int() argument must be a string, a bytes-like object or a number, not 'RequestSite'

I’m encountering an internal server error. The error message indicates a TypeError related to the int() function and the id field.
Here is the stack trace:

[root] [user None] [ip None] signals.py:22 - Uncaught exception from None
lms_1                        | Traceback (most recent call last):
lms_1                        |   File "/openedx/edx-platform/./openedx/core/djangoapps/content/block_structure/store.py", line 97, in get
lms_1                        |     serialized_data = self._get_from_cache(bs_model)
lms_1                        |   File "/openedx/edx-platform/./openedx/core/djangoapps/content/block_structure/store.py", line 179, in _get_from_cache
lms_1                        |     raise BlockStructureNotFound(bs_model.data_usage_key)
lms_1                        | openedx.core.djangoapps.content.block_structure.exceptions.BlockStructureNotFound: Block structure not found; data_usage_key: block-v1:ACAe+100+backoffice+type@course+block@course
lms_1                        | 
lms_1                        | During handling of the above exception, another exception occurred:
lms_1                        | 
lms_1                        | Traceback (most recent call last):
lms_1                        |   File "/openedx/edx-platform/./openedx/core/djangoapps/content/block_structure/manager.py", line 96, in get_collected
lms_1                        |     block_structure = BlockStructureFactory.create_from_store(
lms_1                        |   File "/openedx/edx-platform/./openedx/core/djangoapps/content/block_structure/factory.py", line 86, in create_from_store
lms_1                        |     return block_structure_store.get(root_block_usage_key)
lms_1                        |   File "/openedx/edx-platform/./openedx/core/djangoapps/content/block_structure/store.py", line 99, in get
lms_1                        |     serialized_data = self._get_from_store(bs_model)
lms_1                        |   File "/openedx/edx-platform/./openedx/core/djangoapps/content/block_structure/store.py", line 190, in _get_from_store
lms_1                        |     raise BlockStructureNotFound(bs_model.data_usage_key)
lms_1                        | openedx.core.djangoapps.content.block_structure.exceptions.BlockStructureNotFound: Block structure not found; data_usage_key: block-v1:ACAe+100+backoffice+type@course+block@course
lms_1                        | 
lms_1                        | During handling of the above exception, another exception occurred:
lms_1                        | 
lms_1                        | Traceback (most recent call last):
lms_1                        |   File "/openedx/venv/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 1823, in get_prep_value
lms_1                        |     return int(value)
lms_1                        | TypeError: int() argument must be a string, a bytes-like object or a number, not 'RequestSite'
lms_1                        | 
lms_1                        | The above exception was the direct cause of the following exception:
lms_1                        | 
lms_1                        | Traceback (most recent call last):
lms_1                        |   File "/openedx/venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
lms_1                        |     response = get_response(request)
lms_1                        |   File "/openedx/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
lms_1                        |     response = wrapped_callback(request, *callback_args, **callback_kwargs)
lms_1                        |   File "/opt/pyenv/versions/3.8.12/lib/python3.8/contextlib.py", line 75, in inner
lms_1                        |     return func(*args, **kwds)
lms_1                        |   File "/openedx/venv/lib/python3.8/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
lms_1                        |     return view_func(request, *args, **kwargs)
lms_1                        |   File "/openedx/venv/lib/python3.8/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
lms_1                        |     response = view_func(request, *args, **kwargs)
lms_1                        |   File "/openedx/edx-platform/./openedx/core/djangoapps/util/maintenance_banner.py", line 42, in _decorated
lms_1                        |     return func(request, *args, **kwargs)
lms_1                        |   File "/openedx/edx-platform/common/djangoapps/student/views/dashboard.py", line 876, in student_dashboard
lms_1                        |     for url in get_resume_urls_for_enrollments(user, course_enrollments).values():
lms_1                        |   File "/openedx/edx-platform/common/djangoapps/student/helpers.py", line 781, in get_resume_urls_for_enrollments
lms_1                        |     block_data = get_course_blocks(user, block_key)
lms_1                        |   File "/openedx/edx-platform/./lms/djangoapps/course_blocks/api.py", line 107, in get_course_blocks
lms_1                        |     return get_block_structure_manager(starting_block_usage_key.course_key).get_transformed(
lms_1                        |   File "/openedx/edx-platform/./openedx/core/djangoapps/content/block_structure/manager.py", line 65, in get_transformed
lms_1                        |     block_structure = collected_block_structure.copy() if collected_block_structure else self.get_collected()
lms_1                        |   File "/openedx/edx-platform/./openedx/core/djangoapps/content/block_structure/manager.py", line 105, in get_collected
lms_1                        |     block_structure = self._update_collected()
lms_1                        |   File "/openedx/edx-platform/./openedx/core/djangoapps/content/block_structure/manager.py", line 128, in _update_collected
lms_1                        |     BlockStructureTransformers.collect(block_structure)
lms_1                        |   File "/openedx/edx-platform/./openedx/core/djangoapps/content/block_structure/transformers.py", line 78, in collect
lms_1                        |     transformer.collect(block_structure)
lms_1                        |   File "/openedx/edx-platform/./lms/djangoapps/course_blocks/transformers/user_partitions.py", line 56, in collect
lms_1                        |     user_partitions = get_all_partitions_for_course(root_block, active_only=True)
lms_1                        |   File "/openedx/edx-platform/./openedx/core/lib/cache_utils.py", line 74, in decorator
lms_1                        |     result = wrapped(*args, **kwargs)
lms_1                        |   File "/openedx/edx-platform/xmodule/partitions/partitions_service.py", line 41, in get_all_partitions_for_course
lms_1                        |     all_partitions = course.user_partitions + _get_dynamic_partitions(course)
lms_1                        |   File "/openedx/edx-platform/xmodule/partitions/partitions_service.py", line 84, in _get_dynamic_partitions
lms_1                        |     generated_partition = generator(course)
lms_1                        |   File "/openedx/edx-platform/./openedx/features/content_type_gating/partitions.py", line 38, in create_content_gating_partition
lms_1                        |     studio_override_for_course = ContentTypeGatingConfig.current(course_key=course.id).studio_override_enabled
lms_1                        |   File "/openedx/edx-platform/./openedx/core/djangoapps/config_model_utils/models.py", line 199, in current
lms_1                        |     overrides = cls.objects.current_set().filter(multi_filter_query)
lms_1                        |   File "/openedx/venv/lib/python3.8/site-packages/django/db/models/query.py", line 941, in filter
lms_1                        |     return self._filter_or_exclude(False, args, kwargs)
lms_1                        |   File "/openedx/venv/lib/python3.8/site-packages/django/db/models/query.py", line 961, in _filter_or_exclude
lms_1                        |     clone._filter_or_exclude_inplace(negate, args, kwargs)
lms_1                        |   File "/openedx/venv/lib/python3.8/site-packages/django/db/models/query.py", line 968, in _filter_or_exclude_inplace
lms_1                        |     self._query.add_q(Q(*args, **kwargs))
lms_1                        |   File "/openedx/venv/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1416, in add_q
lms_1                        |     clause, _ = self._add_q(q_object, self.used_aliases)
lms_1                        |   File "/openedx/venv/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1435, in _add_q
lms_1                        |     child_clause, needed_inner = self.build_filter(
lms_1                        |   File "/openedx/venv/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1288, in build_filter
lms_1                        |     return self._add_q(
lms_1                        |   File "/openedx/venv/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1435, in _add_q
lms_1                        |     child_clause, needed_inner = self.build_filter(
lms_1                        |   File "/openedx/venv/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1288, in build_filter
lms_1                        |     return self._add_q(
lms_1                        |   File "/openedx/venv/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1435, in _add_q
lms_1                        |     child_clause, needed_inner = self.build_filter(
lms_1                        |   File "/openedx/venv/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1370, in build_filter
lms_1                        |     condition = self.build_lookup(lookups, col, value)
lms_1                        |   File "/openedx/venv/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1216, in build_lookup
lms_1                        |     lookup = lookup_class(lhs, rhs)
lms_1                        |   File "/openedx/venv/lib/python3.8/site-packages/django/db/models/lookups.py", line 25, in __init__
lms_1                        |     self.rhs = self.get_prep_lookup()
lms_1                        |   File "/openedx/venv/lib/python3.8/site-packages/django/db/models/fields/related_lookups.py", line 117, in get_prep_lookup
lms_1                        |     self.rhs = target_field.get_prep_value(self.rhs)
lms_1                        |   File "/openedx/venv/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 1825, in get_prep_value
lms_1                        |     raise e.__class__(
lms_1                        | TypeError: Field 'id' expected a number but got <django.contrib.sites.requests.RequestSite object at 0x7fabae258490>.
lms_1                        | 2023-06-29 22:35:40,607 ERROR 19 [django.request] [user 4] [ip 37.252.89.251] log.py:224 - Internal Server Error: /dashboard
lms_1                        | Traceback (most recent call last):
lms_1                        |   File "/openedx/edx-platform/./openedx/core/djangoapps/content/block_structure/store.py", line 97, in get
lms_1                        |     serialized_data = self._get_from_cache(bs_model)
lms_1                        |   File "/openedx/edx-platform/./openedx/core/djangoapps/content/block_structure/store.py", line 179, in _get_from_cache
lms_1                        |     raise BlockStructureNotFound(bs_model.data_usage_key)
lms_1                        | openedx.core.djangoapps.content.block_structure.exceptions.BlockStructureNotFound: Block structure not found; data_usage_key: block-v1:ACAe+100+backoffice+type@course+block@course
lms_1                        | 
lms_1                        | During handling of the above exception, another exception occurred:
lms_1                        | 
lms_1                        | Traceback (most recent call last):
lms_1                        |   File "/openedx/edx-platform/./openedx/core/djangoapps/content/block_structure/manager.py", line 96, in get_collected
lms_1                        |     block_structure = BlockStructureFactory.create_from_store(
lms_1                        |   File "/openedx/edx-platform/./openedx/core/djangoapps/content/block_structure/factory.py", line 86, in create_from_store
lms_1                        |     return block_structure_store.get(root_block_usage_key)
lms_1                        |   File "/openedx/edx-platform/./openedx/core/djangoapps/content/block_structure/store.py", line 99, in get
lms_1                        |     serialized_data = self._get_from_store(bs_model)
lms_1                        |   File "/openedx/edx-platform/./openedx/core/djangoapps/content/block_structure/store.py", line 190, in _get_from_store
lms_1                        |     raise BlockStructureNotFound(bs_model.data_usage_key)
lms_1                        | openedx.core.djangoapps.content.block_structure.exceptions.BlockStructureNotFound: Block structure not found; data_usage_key: block-v1:ACAe+100+backoffice+type@course+block@course
lms_1                        | 
lms_1                        | During handling of the above exception, another exception occurred:
lms_1                        | 
lms_1                        | Traceback (most recent call last):
lms_1                        |   File "/openedx/venv/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 1823, in get_prep_value
lms_1                        |     return int(value)
lms_1                        | TypeError: int() argument must be a string, a bytes-like object or a number, not 'RequestSite'
lms_1                        | 
lms_1                        | The above exception was the direct cause of the following exception:
lms_1                        | 
lms_1                        | Traceback (most recent call last):
lms_1                        |   File "/openedx/venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
lms_1                        |     response = get_response(request)
lms_1                        |   File "/openedx/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
lms_1                        |     response = wrapped_callback(request, *callback_args, **callback_kwargs)
lms_1                        |   File "/opt/pyenv/versions/3.8.12/lib/python3.8/contextlib.py", line 75, in inner
lms_1                        |     return func(*args, **kwds)
lms_1                        |   File "/openedx/venv/lib/python3.8/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
lms_1                        |     return view_func(request, *args, **kwargs)
lms_1                        |   File "/openedx/venv/lib/python3.8/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
lms_1                        |     response = view_func(request, *args, **kwargs)
lms_1                        |   File "/openedx/edx-platform/./openedx/core/djangoapps/util/maintenance_banner.py", line 42, in _decorated
lms_1                        |     return func(request, *args, **kwargs)
lms_1                        |   File "/openedx/edx-platform/common/djangoapps/student/views/dashboard.py", line 876, in student_dashboard
lms_1                        |     for url in get_resume_urls_for_enrollments(user, course_enrollments).values():
lms_1                        |   File "/openedx/edx-platform/common/djangoapps/student/helpers.py", line 781, in get_resume_urls_for_enrollments
lms_1                        |     block_data = get_course_blocks(user, block_key)
lms_1                        |   File "/openedx/edx-platform/./lms/djangoapps/course_blocks/api.py", line 107, in get_course_blocks
lms_1                        |     return get_block_structure_manager(starting_block_usage_key.course_key).get_transformed(
lms_1                        |   File "/openedx/edx-platform/./openedx/core/djangoapps/content/block_structure/manager.py", line 65, in get_transformed
lms_1                        |     block_structure = collected_block_structure.copy() if collected_block_structure else self.get_collected()
lms_1                        |   File "/openedx/edx-platform/./openedx/core/djangoapps/content/block_structure/manager.py", line 105, in get_collected
lms_1                        |     block_structure = self._update_collected()
lms_1                        |   File "/openedx/edx-platform/./openedx/core/djangoapps/content/block_structure/manager.py", line 128, in _update_collected
lms_1                        |     BlockStructureTransformers.collect(block_structure)
lms_1                        |   File "/openedx/edx-platform/./openedx/core/djangoapps/content/block_structure/transformers.py", line 78, in collect
lms_1                        |     transformer.collect(block_structure)
lms_1                        |   File "/openedx/edx-platform/./lms/djangoapps/course_blocks/transformers/user_partitions.py", line 56, in collect
lms_1                        |     user_partitions = get_all_partitions_for_course(root_block, active_only=True)
lms_1                        |   File "/openedx/edx-platform/./openedx/core/lib/cache_utils.py", line 74, in decorator
lms_1                        |     result = wrapped(*args, **kwargs)
lms_1                        |   File "/openedx/edx-platform/xmodule/partitions/partitions_service.py", line 41, in get_all_partitions_for_course
lms_1                        |     all_partitions = course.user_partitions + _get_dynamic_partitions(course)
lms_1                        |   File "/openedx/edx-platform/xmodule/partitions/partitions_service.py", line 84, in _get_dynamic_partitions
lms_1                        |     generated_partition = generator(course)
lms_1                        |   File "/openedx/edx-platform/./openedx/features/content_type_gating/partitions.py", line 38, in create_content_gating_partition
lms_1                        |     studio_override_for_course = ContentTypeGatingConfig.current(course_key=course.id).studio_override_enabled
lms_1                        |   File "/openedx/edx-platform/./openedx/core/djangoapps/config_model_utils/models.py", line 199, in current
lms_1                        |     overrides = cls.objects.current_set().filter(multi_filter_query)
lms_1                        |   File "/openedx/venv/lib/python3.8/site-packages/django/db/models/query.py", line 941, in filter
lms_1                        |     return self._filter_or_exclude(False, args, kwargs)
lms_1                        |   File "/openedx/venv/lib/python3.8/site-packages/django/db/models/query.py", line 961, in _filter_or_exclude
lms_1                        |     clone._filter_or_exclude_inplace(negate, args, kwargs)
lms_1                        |   File "/openedx/venv/lib/python3.8/site-packages/django/db/models/query.py", line 968, in _filter_or_exclude_inplace
lms_1                        |     self._query.add_q(Q(*args, **kwargs))
lms_1                        |   File "/openedx/venv/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1416, in add_q
lms_1                        |     clause, _ = self._add_q(q_object, self.used_aliases)
lms_1                        |   File "/openedx/venv/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1435, in _add_q
lms_1                        |     child_clause, needed_inner = self.build_filter(
lms_1                        |   File "/openedx/venv/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1288, in build_filter
lms_1                        |     return self._add_q(
lms_1                        |   File "/openedx/venv/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1435, in _add_q
lms_1                        |     child_clause, needed_inner = self.build_filter(
lms_1                        |   File "/openedx/venv/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1288, in build_filter
lms_1                        |     return self._add_q(
lms_1                        |   File "/openedx/venv/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1435, in _add_q
lms_1                        |     child_clause, needed_inner = self.build_filter(
lms_1                        |   File "/openedx/venv/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1370, in build_filter
lms_1                        |     condition = self.build_lookup(lookups, col, value)
lms_1                        |   File "/openedx/venv/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1216, in build_lookup
lms_1                        |     lookup = lookup_class(lhs, rhs)
lms_1                        |   File "/openedx/venv/lib/python3.8/site-packages/django/db/models/lookups.py", line 25, in __init__
lms_1                        |     self.rhs = self.get_prep_lookup()
lms_1                        |   File "/openedx/venv/lib/python3.8/site-packages/django/db/models/fields/related_lookups.py", line 117, in get_prep_lookup
lms_1                        |     self.rhs = target_field.get_prep_value(self.rhs)
lms_1                        |   File "/openedx/venv/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 1825, in get_prep_value
lms_1                        |     raise e.__class__(
lms_1                        | TypeError: Field 'id' expected a number but got <django.contrib.sites.requests.RequestSite object at 0x7fabae258490>.

It occurs in the /api/course_home/course_metadata/some-course endpoint, and it seems that the code is expecting an integer value for the id field but is receiving a RequestSite object instead.

I was trying to integrate jupyterhub but don’t sure that it can cause this errors.
Has anyone encountered a similar issue before? What could be causing this error? Are there any specific steps I should take to investigate and resolve this problem? Any help or guidance would be greatly appreciated.

Thank you!

Hey @M_M , were you able to solve this issue? I am also facing the same error. Any help will be appreciated.

Thank you.

Alright, after debugging I found that the code tries to look for site with hard coded id as 1 when looking for site configuration. If site with id 1 isn’t available then it returns RequestSite object.

I remember when setting up edx app, by default site table contained one record with domain example.com having site id as 1. Unfortunately, I deleted that thinking it won’t be of any use.

I noticed that SITE_ID is hard coded as 1 in the application. I just re-inserted the record with id 1 and voila!
Instead of deleting that record, I should have simply changed it to match the required domain.

Hope, this finding gives other direction to look for when facing similar problem.