How can I see data from an already made mysql database on Django?

Viewed 744

I'm new to Django and I am trying to use a mysql database created and filled with data by someone else

I created a model with the same name as the table I want to get data from, my models is as follows

class Study(models.Model):
study_name =        models.TextField(default='Unknown')
description = models.TextField(default='Unknown')
language =    models.TextField(default='Unknown')
number_of_years = models.IntegerField(default='0')

database connected but when I go to admin I don't see the data there

Please help me with this

3 Answers

You should create ModelAdmin instance for your model

The ModelAdmin class is the representation of a model in the admin interface. Usually, these are stored in a file named admin.py in your application.

from django.contrib import admin
from myproject.myapp.models import Study

class StudyAdmin(admin.ModelAdmin):
    pass
admin.site.register(Study, StudyAdmin)

A step by step solution would be:

  1. get the name of the table containing your data, I'll call it study_table
  2. make sure you know how the table was defined so you can match it with django model definition. Connect to the database with a MySQL client and run the following query:
    DESCRIBE study_table;
    
  3. based on the table name, column types and column names, define your model to match everything. Django models do a lot of automated naming so you have force the naming to make sure your model matches your database. Principles are:

    • Specify the table name as a meta option.
    • Create fields with names matching column names and field types matching column types. Taking an example from your code, the field study_name should match a column with the same name in the table study_table.

    class Study(models.Model):
    study_name = models.TextField(default='Unknown')
    description = models.TextField(default='Unknown')
    language = models.TextField(default='Unknown')
    number_of_years = models.IntegerField(default=0)
    
    class Meta:
        db_table = study_table
    

    Side note: your IntegerField has a default as a string '0'.

  4. making sure the app (I'll call it study_app) containing your model is enabled, the database is configure properly in your django settings, try to access data from the admin shell (python manage.py shell):
    >>> from study_app.models import Study
    >>> Study.objects.first()
    
    This should return an answer, if it does not, your model doesn't match the database data.
  5. to make accessing the data easier, create an admin page as suggested by @iklinac. You can now read, edit your data through your browser.

A few suggestions you could consider:

  • study_name should probably be a models.CharField(max_length=255) or similar
  • description should be allowed to be empty models.TextField(blank=True)
  • language should probably be a models.CharField with a choices option.

If you have a MySQL database with tables of data that don't have models created yet, you can use the dumpdata command to automatically generate the models:

https://docs.djangoproject.com/en/3.0/ref/django-admin/#dumpdata

Then, you can register those models in the Django admin. dumpdata should only be used as a starting point, since they are auto-generated and won't contain many of Django's data integrity features.

Good luck!

Related