0

I have a MusicAlbum which might have not just one genre related to it. Within my import process I have the following function defined to first get the genre and second, create a relation between the MusicAlbum and the Genre:

def pull_genre(object_id):
    genre_list = []
    split_content = [meta.strip() for meta in genre_value.split(',')]
    genre_list.append(split_content)
    for genre in genre_list:
        new_genre, create = object_id.genre_relation.get_or_create(
            name=genre,
            object_id=object_id,
        )

Problem now is that the following gets written to my Database:

['Rock', 'Metal']

But what I want is not a list stored inside my database, instead I want to have separate objects at my DB. One for Rock the other for Metal, what I'm doing wrong here?

0

2 Answers 2

1

You are iterating a list (genre_list) which contains (because you append() to it) another list (split_content). split_content already is a list, containing an entry for every entry returned from genre_value.split(','). Use it directly instead of putting it inside another list:

def pull_genre(object_id):
    split_content = [meta.strip() for meta in genre_value.split(',')]

    for genre in split_content:
        new_genre, create = object_id.genre_relation.get_or_create(
            name=genre,
            object_id=object_id,
        )

However, I'm not quite sure where you're pulling genre_value from, so you might want to take a closer look at the design of your script.

Sign up to request clarification or add additional context in comments.

Comments

0
def pull_genre(object_id):
    split_content = genre_value.split(',')
    for genre in split_content:
        genre_striped = genre.strip()
        # Do other stuff on genre_striped
        new_genre, create = object_id.genre_relation.get_or_create(
            name=genre_striped,
            object_id=object_id,
        )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.