Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImageField in deeply nested inlines is not working #115

Open
henrygaurav opened this issue Apr 9, 2020 · 2 comments
Open

ImageField in deeply nested inlines is not working #115

henrygaurav opened this issue Apr 9, 2020 · 2 comments

Comments

@henrygaurav
Copy link

Image uploading in the first inline-level works fine, but in the second level or third level, it gets ignored. Nothing fails and the rest of the fields data get saved fine, but the file upload doesn't happen, the file is not in the server, and the corresponding entry in the database is not stored.

BTW, this is using Django 2.2.1 and code like the following:

models.py
class Course(models.Model):
name = models.CharField(max_length=255)

class Book(models.Model):
name = models.CharField(max_length=255)
course = models.ForeignKey(Course, on_delete=models.CASCADE)

class Lesson(models.Model):
name = models.CharField(max_length=255)
book = models.ForeignKey(Book, on_delete=models.CASCADE)

class LessonImage(models.Model):
image = models.ImageField(null=True, blank=True, upload_to="uploads/")
lesson = models.ForeignKey(Lesson, on_delete=models.CASCADE, related_name='lesson_image')

admin.py
class LessonImageInline(NestedStackedInline):
model = LessonImage
extra = 1
min_num = 0

class LessonInline(NestedStackedInline):
model = Lesson
extra = 1
min_num = 0
inlines = [LessonImageInline]

class BookInline(NestedStackedInline):
model = Book
extra = 1
min_num = 0
inlines = [LessonInline]

@admin.register(Course)
class CourseAdmin(NestedModelAdmin):
list_display = ('created_at', 'updated_at')
list_per_page = 50
inlines = [BookInline]

@pljspahn
Copy link

pljspahn commented Apr 30, 2020

Seeing the same problem. My models aren't quite set up like yours, but similar.

When creating a brand new record in admin, the image doesn't save but all values do save ... KIND OF!

Not only does the image get ignored and not saved (I'm using S3 storage for images), but despite the other fields showing values in the admin, the related model's data isn't provided in either ListView or DetailView.

The only workaround that seems to work is to explicitly save the record twice. Create record like normal, filling out the fields, then hit 'save and continue editing' then add the image and then save again. Only after saving the record TWICE does the data get populated in ListView and DetailView, otherwise my {% for thing in things.all %} loop will trigger {% empty %}

@henrygaurav
Copy link
Author

henrygaurav commented Apr 30, 2020

Image is not saving because the enctype="multipart/form-data" attribute is missing in the form tag. So I found a workaround. I override the is_multipart method and returned true.

class LessonForm(forms.ModelForm):
    def is_multipart(self):
        return True
class LessonAdmin(NestedModelAdmin):
    form = LessonForm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants