[Fixed]-Django – Access Parent Model field when creating many-to-one model

1👍

This is really a question about upload_to. That attribute also takes a callable, which is passed the instance and the filename:

def upload_to_book_path(instance, filename):
    return 'uploads/' + instance.chapter.book.name + '/chapter/' + filename

class ChapterImages(models.Model):
    image = models.ImageField(upload_to=upload_to_book_path)

Leave a comment