0

I have searched and tried everything but cannot get my csv file to populate and show up in Django Admin. Please note all the files below are in the same directory, including the csv.

Here is my models.py

from django.db import models


class Album(models.Model):
    artist = models.CharField(max_length=100)
    album_title = models.CharField(max_length=100)
    genre = models.CharField(max_length=100)
    album_logo = models.CharField(max_length=1000)

    def __str__(self):
        return self.album_title + ' - ' + self.artist

    class Meta:
        verbose_name_plural = 'Album'

and here is my import.py file:

import csv
from music.models import Album

with open('names.csv', 'r') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        artist = row['artist']
        album_title = row['album_title']
        genre = row['genre']
        album_logo = row['album_logo']

        new_album = Album(artist=artist, album_title=album_title, genre=genre, album_logo=album_logo)
        new_album.save()

I run the server and Admin has the table and database, but it's not populated with my csv file's data. Any help would be appreciated. Thank you.

Cyrlop
  • 1,894
  • 1
  • 17
  • 31
Emilio
  • 1
  • 1

1 Answers1

0

The problem is that your import.py is never executed. I assume this is a one time only? It should not be executed every time your Django server stats.

To execute your code, you can either do it from the shell (see this answer):

manage.py shell < import.py

Or use a custom management command.

Cyrlop
  • 1,894
  • 1
  • 17
  • 31