Posts

Showing posts with the label django-tutorial

AttributeError: Got AttributeError when attempting to get a value for field `field_name` on serializer `ModelSerializer`.

The serializer field might be named incorrectly and not match any attribute or key on the `QuerySet` instance. Original exception text was: 'QuerySet' object has no attribute 'name'. Solution: just add many=True on serializer class instance Replace: serializer = ModelSerializer(queryset) to: serializer = ModelSerializer(queryset, many=True) return Response(serializer.data)

import dotenv: How to use dotenv (.env) in django ?

1. First install : pip install django-dotenv 2.   Manage.py file import dotenv and add a line dotenv . read_dotenv ()  in main function import os import sys import dotenv     #new def main (): dotenv . read_dotenv ()     #New os . environ . setdefault ( 'DJANGO_SETTINGS_MODULE' , 'xpertstreet.settings' ) try : from django . core . management import execute_from_command_line except ImportError as exc : 3. Create a file .env with the app (where the all apps are available) .env file Contains: ALLOWED_HOSTS = "13.233.156.17,localhost" DB_NAME = "wooxpertstreet" DB_USER = "postgres" DB_PASS = "12345" DB_HOST = "localhost" DB_PORT = 5432 4. How to get .env values in settings.py or anywhere? import os DATABASES = {     'default' : {         'ENGINE' : 'django.db.backends.postgresql_psycopg2' ,         'NAME' : os . environ . get ( "DB_NAME...