Open obaidullahzaland opened 3 years ago
Hi @ @obaidullahzaland use djongo models to create your models class
Faced the same problem.
"""
Django settings for anime_die_heart project.
Generated by 'django-admin startproject' using Django 4.0.5.
For more information on this file, see
https://docs.djangoproject.com/en/4.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.0/ref/settings/
"""
from pathlib import Path
import os
from django.core.exceptions import ImproperlyConfigured
from shared.read_env import read_env_files
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
BASE_URL = 'http://localhost:8000/'
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-77hptzt_iz9g#0(5jxv&f%+@klssg@si()%3w)3#x@$$g$9n^u'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
def get_env_value(env_variable):
try:
path = os.path.join(os.getcwd(), '.mongodb.env')
env_value = read_env_files([path])[env_variable]
return env_value
except KeyError:
error_msg = f'Set the {env_variable} environment variable'
raise ImproperlyConfigured(error_msg)
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'watch_list_app'
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'anime_die_heart.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'anime_die_heart.wsgi.application'
# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
},
'mongodb': {
'ENGINE': 'djongo',
'NAME': get_env_value('MONGODB_DATABASE'),
'CLIENT': {
'host': 'localhost',
'port': 27017,
'username': get_env_value('MONGODB_USERNAME'),
'password': get_env_value('MONGODB_PASSWORD'),
'authSource': 'admin',
'authMechanism': 'SCRAM-SHA-1'
}
}
}
DEFAULT_FILE_STORAGE = 'mongo_storage.storage.GridFSStorage'
GRIDFS_DATABASE = 'anime_videos'
UPLOADED_FILES_USE_URL = True
# Password validation
# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/4.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/
STATIC_URL = 'static/'
# Default primary key field type
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
from django.db import models
from django.db.models import FileField
from django.db.models.indexes import Index
from djongo.storage import GridFSStorage
from djongo import models as djongoModels
import anime_die_heart.settings as settings
grid_fs_storage = GridFSStorage(
collection='myfiles',
base_url=''.join([settings.BASE_URL, 'myfiles/'])
)
# Create your models here.
class Movie(models.Model):
name: str = models.CharField(max_length=100, null=False)
description: str = models.CharField(max_length=400, null=False)
active: bool = models.BooleanField(default=True)
class Meta:
db_table: str = 'movies'
indexes: Index = [
models.Index(fields=['name'], name='movie_name_index')
]
def __str__(self):
return self.name
class MovieFile(djongoModels.Model):
file: FileField = models.FileField(upload_to='animes', storage=grid_fs_storage)
from rest_framework import serializers
from django.core.files import File
from rest_framework.serializers import FileField
from ..models import MovieFile
class MovieSerializer(serializers.Serializer):
id: int = serializers.IntegerField(read_only=True, required=False)
name: str = serializers.CharField(required=True)
description: str = serializers.CharField(required=True)
active: bool = serializers.BooleanField(required=False)
class UploadMovieSerializer(serializers.Serializer):
file: File = serializers.FileField(required=True)
from rest_framework.exceptions import bad_request
from zoneinfo import ZoneInfo
from rest_framework import status
from django.core.files import File
import datetime
from rest_framework.response import Response
from rest_framework.decorators import api_view
from rest_framework.request import Request
from ..models import Movie
from ..models import MovieFile
from .serializers import MovieSerializer
from .serializers import UploadMovieSerializer
@api_view(["POST"])
def upload_movie(req: Request) -> Response:
if 'file' not in req.FILES:
return bad_request(req, 'Please select a file')
serialized_movie = UploadMovieSerializer(data=req.data)
"""
Wrong usage
<MultiValueDict: {'file': [<InMemoryUploadedFile: Untitled.png (image/png)>]}>
req.FILES.file
"""
sent_file: File = req.FILES['file']
MovieFile.objects.create(file=sent_file)
# save_uploaded_file(sent_file)
# Timezone is really a hard thing to deal. So I decided to keep it in zero timezone
now = datetime.datetime.now(tz=ZoneInfo("Etc/GMT"))
created_move = Movie.objects.create(
name=sent_file.name,
description=f"File uploaded at {now}",
active=True
)
created_move.save()
serialized_movie = MovieSerializer(created_move)
return Response(
serialized_movie.data,
status=status.HTTP_201_CREATED
)
Traceback (most recent call last):
File "/home/kasir/anime_die_heart/venv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
File "/home/kasir/anime_die_heart/venv/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/kasir/anime_die_heart/venv/lib/python3.10/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/kasir/anime_die_heart/venv/lib/python3.10/site-packages/django/views/generic/base.py", line 84, in view
return self.dispatch(request, *args, **kwargs)
File "/home/kasir/anime_die_heart/venv/lib/python3.10/site-packages/rest_framework/views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "/home/kasir/anime_die_heart/venv/lib/python3.10/site-packages/rest_framework/views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/kasir/anime_die_heart/venv/lib/python3.10/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
raise exc
File "/home/kasir/anime_die_heart/venv/lib/python3.10/site-packages/rest_framework/views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "/home/kasir/anime_die_heart/venv/lib/python3.10/site-packages/rest_framework/decorators.py", line 50, in handler
return func(*args, **kwargs)
File "/home/kasir/anime_die_heart/anime_die_heart/watch_list_app/api/views.py", line 62, in upload_movie
MovieFile.objects.create(file=sent_file)
File "/home/kasir/anime_die_heart/venv/lib/python3.10/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/kasir/anime_die_heart/venv/lib/python3.10/site-packages/django/db/models/query.py", line 514, in create
obj.save(force_insert=True, using=self.db)
File "/home/kasir/anime_die_heart/venv/lib/python3.10/site-packages/django/db/models/base.py", line 806, in save
self.save_base(
File "/home/kasir/anime_die_heart/venv/lib/python3.10/site-packages/django/db/models/base.py", line 857, in save_base
updated = self._save_table(
File "/home/kasir/anime_die_heart/venv/lib/python3.10/site-packages/django/db/models/base.py", line 1000, in _save_table
results = self._do_insert(
File "/home/kasir/anime_die_heart/venv/lib/python3.10/site-packages/django/db/models/base.py", line 1041, in _do_insert
return manager._insert(
File "/home/kasir/anime_die_heart/venv/lib/python3.10/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/kasir/anime_die_heart/venv/lib/python3.10/site-packages/django/db/models/query.py", line 1434, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "/home/kasir/anime_die_heart/venv/lib/python3.10/site-packages/django/db/models/sql/compiler.py", line 1620, in execute_sql
for sql, params in self.as_sql():
File "/home/kasir/anime_die_heart/venv/lib/python3.10/site-packages/django/db/models/sql/compiler.py", line 1547, in as_sql
value_rows = [
File "/home/kasir/anime_die_heart/venv/lib/python3.10/site-packages/django/db/models/sql/compiler.py", line 1548, in <listcomp>
[
File "/home/kasir/anime_die_heart/venv/lib/python3.10/site-packages/django/db/models/sql/compiler.py", line 1549, in <listcomp>
self.prepare_value(field, self.pre_save_val(field, obj))
File "/home/kasir/anime_die_heart/venv/lib/python3.10/site-packages/django/db/models/sql/compiler.py", line 1497, in pre_save_val
return field.pre_save(obj, add=True)
File "/home/kasir/anime_die_heart/venv/lib/python3.10/site-packages/django/db/models/fields/files.py", line 316, in pre_save
file.save(file.name, file.file, save=False)
File "/home/kasir/anime_die_heart/venv/lib/python3.10/site-packages/django/db/models/fields/files.py", line 92, in save
self.name = self.storage.save(name, content, max_length=self.field.max_length)
File "/home/kasir/anime_die_heart/venv/lib/python3.10/site-packages/django/core/files/storage.py", line 56, in save
name = self.get_available_name(name, max_length=max_length)
File "/home/kasir/anime_die_heart/venv/lib/python3.10/site-packages/django/core/files/storage.py", line 96, in get_available_name
while self.exists(name) or (max_length and len(name) > max_length):
File "/home/kasir/anime_die_heart/venv/lib/python3.10/site-packages/djongo/storage.py", line 110, in exists
gridfs, filename = self._get_gridfs(path)
File "/home/kasir/anime_die_heart/venv/lib/python3.10/site-packages/djongo/storage.py", line 168, in _get_gridfs
return GridFS(self._db, collection_name), filename
File "/home/kasir/anime_die_heart/venv/lib/python3.10/site-packages/gridfs/__init__.py", line 80, in __init__
raise TypeError("database must be an instance of Database")
TypeError: database must be an instance of Database
[01/Jul/2022 06:56:39] "POST /movies/upload HTTP/1.1" 500 187371
@shreyanshu7101904 @obaidullahzaland
Did you knwo what is wrong?
@shreyanshu7101904 @obaidullahzaland
When I remove the "default"
from settings.py it throw this error:
pymongo.errors.OperationFailure: Authentication failed., full error: {'ok': 0.0, 'errmsg': 'Authentication failed.', 'code': 18, 'codeName': 'AuthenticationFailed'}
I guess I do not understand how settings.py
exactly works.
Please see here for a better understanding about why I was getting Authentication Failed: https://github.com/kasir-barati/docker/tree/docker-compose/docker-compose-files/mongodb
I am using the following libraries asgiref==3.3.4 Django==3.2.4 django-cors-headers==3.7.0 djangorestframework==3.12.4 djongo==1.3.6 pymongo==3.11.4 pytz==2021.1 sqlparse==0.2.4
My models.py looks like
from django.db import models
Create your models here.
class Tutorial(models.Model): title = models.CharField(max_length=70, blank=True, default='') description = models.CharField(max_length=200, blank=True, default='') published = models.BooleanField(default=True)
I have also updated the required settings in the SETTINGS,PY file to use Djongo as my backed but when I use the following command python manage.py makemigrations, it throws the following error..
(djangomongo) D:\django_practice\DjangoMongoAngular\DjangoRestAPIMongoDB>python manage.py makemigrations Traceback (most recent call last): File "D:\django_practice\DjangoMongoAngular\DjangoRestAPIMongoDB\manage.py", line 22, in
main()
File "D:\django_practice\DjangoMongoAngular\DjangoRestAPIMongoDB\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "D:\django_practice\DjangoMongoAngular\djangomongo\lib\site-packages\django\core\management__init.py", line 419, in execute_from_command_line
utility.execute()
File "D:\django_practice\DjangoMongoAngular\djangomongo\lib\site-packages\django\core\management__init.py", line 413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv) File "D:\django_practice\DjangoMongoAngular\djangomongo\lib\site-packages\django\core\management\base.py", line 354, in run_from_argv
self.execute(*args, cmd_options) File "D:\django_practice\DjangoMongoAngular\djangomongo\lib\site-packages\django\core\management\base.py", line 398, in execute output = self.handle(*args, *options) File "D:\django_practice\DjangoMongoAngular\djangomongo\lib\site-packages\django\core\management\base.py", line 89, in wrapped res = handle_func(args, kwargs) File "D:\django_practice\DjangoMongoAngular\djangomongo\lib\site-packages\django\core\management\commands\makemigrations.py", line 103, in handle loader.check_consistent_history(connection) File "D:\django_practice\DjangoMongoAngular\djangomongo\lib\site-packages\django\db\migrations\loader.py", line 294, in check_consistent_history applied = recorder.applied_migrations() File "D:\django_practice\DjangoMongoAngular\djangomongo\lib\site-packages\django\db\migrations\recorder.py", line 77, in applied_migrations if self.has_table(): File "D:\django_practice\DjangoMongoAngular\djangomongo\lib\site-packages\django\db\migrations\recorder.py", line 55, in has_table with self.connection.cursor() as cursor: File "D:\django_practice\DjangoMongoAngular\djangomongo\lib\site-packages\django\utils\asyncio.py", line 26, in inner return func(*args, kwargs) File "D:\django_practice\DjangoMongoAngular\djangomongo\lib\site-packages\django\db\backends\base\base.py", line 259, in cursor return self._cursor() File "D:\django_practice\DjangoMongoAngular\djangomongo\lib\site-packages\django\db\backends\base\base.py", line 235, in _cursor self.ensure_connection() File "D:\django_practice\DjangoMongoAngular\djangomongo\lib\site-packages\django\utils\asyncio.py", line 26, in inner return func(*args, *kwargs) File "D:\django_practice\DjangoMongoAngular\djangomongo\lib\site-packages\django\db\backends\base\base.py", line 219, in ensure_connection self.connect() File "D:\django_practice\DjangoMongoAngular\djangomongo\lib\site-packages\django\utils\asyncio.py", line 26, in inner return func(args, kwargs) File "D:\django_practice\DjangoMongoAngular\djangomongo\lib\site-packages\django\db\backends\base\base.py", line 200, in connect self.connection = self.get_new_connection(conn_params) File "D:\django_practice\DjangoMongoAngular\djangomongo\lib\site-packages\djongo\base.py", line 179, in get_new_connection database = self.client_connection[name] File "D:\django_practice\DjangoMongoAngular\djangomongo\lib\site-packages\pymongo\mongo_client.py", line 1569, in getitem__ return database.Database(self, name) File "D:\django_practice\DjangoMongoAngular\djangomongo\lib\site-packages\pymongo\database.py", line 107, in init__ raise TypeError("name must be an instance " TypeError: name must be an instance of str