How can I display all my existing records in Mysql from Django database? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How can I display all my existing records in Mysql from Django database? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I migrated the Django database model to MySQL. When I tried to access the data in MySQL, I can’t seem to find my existing data. How can I fix this?

Migration code:

settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'sample',
        'USER': '********',
        "PASSWORD": '********',
        'HOST': '*****',
        'PORT': '*****',
        'OPTIONS': {
        'sql_mode': 'traditional',
        }
    }
}

models.py

from django.db import models
import os
from PIL import Image
from datetime import date
import datetime
from .validators import validate_file_extension
import base64
from django.utils.functional import cached_property
from django.utils.html import format_html



def get_directory_path(instance, filename):
    today = date.today()
    t = datetime.datetime.now() 
    day, month, year = today.day, today.month, today.year
    hour, minutes, seconds = t.hour, t.minute, t.second
    filename = str(day) + str(month) + str(year) + str(hour) + str(minutes) + str(seconds) + '.png'
    dir = 'media'
    path = '{0}/{1}'.format(dir, filename)
    return path


class Image(models.Model):
    image = models.FileField(upload_to = get_directory_path, null = True , validators=[validate_file_extension])
    created_date = models.DateTimeField(auto_now = True)

    def __str__(self):
        return str(self.id)

After this, I typed:

python manage.py makemigrations
python manage.py migrate

Everything seems to work well but I’m not able to view my data. Any suggestions?

Answer

Were you using sqlite before? After changing the database and migrating only the database schemas(tables) are made in MySql. You will loose all your data from before and cannot retrieve it.

We are here to answer your question about How can I display all my existing records in Mysql from Django database? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji