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

FileFields in deeply nested inlines broken in Django 2.1 #111

Closed
alito opened this issue Sep 20, 2018 · 6 comments
Closed

FileFields in deeply nested inlines broken in Django 2.1 #111

alito opened this issue Sep 20, 2018 · 6 comments

Comments

@alito
Copy link

alito commented Sep 20, 2018

Image uploading in the first inline level works fine, but in the second level it gets ignored. Nothing fails and the rest of the formsets 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.1 and code like the following:

class CatImageInline(nested_admin.NestedStackedInline):
    model = ourmodels.CatImage
    extra = 1

class CatInline(nested_admin.NestedStackedInline):
    model = ourmodels.Cat
    extra = 2

    inlines = [
        CatImageInline,
    ]


@admin.register(ourmodels.FeralCatSurvey)
class FeralCatSurveyAdmin(nested_admin.NestedModelAdmin):
     inlines = [
        CatInline
    ] 

@fdintino
Copy link
Member

For the model with the file field, is that the only field on the form? Or are there other fields in the same inline? If, for instance, there is a charfield, try filling out that field and uploading a file at the same time, to see if that works. If the adjacent charfield saves but the file doesn't upload, that will suggest one issue. If they both save, however, then I have a theory for why that might be (and so, how I would fix it).

@alito
Copy link
Author

alito commented Sep 21, 2018

It was the only field, but I added a text field to test. Interestingly, the form now fails to validate highlighting the Image field and showing "This field is required", as if it doesn't see the file upload at all.

@alito
Copy link
Author

alito commented Sep 24, 2018

Ok, tracked it down a bit. The issue is that Django admin doesn't set 'multipart/form-data' on the HTML form if none of the fields it knows about in the formsets it knows about need multipart. The relevant code bit is in contrib/admin/options.py in render_change_form.
I don't know what the proper fix is, but a simple workaround that seems to work is to set a form on the admin object, where the form just overrides is_multipart. eg:

class FeralCatSurveyForm(forms.ModelForm):
    def is_multipart(self):
       return True

    class Meta:
        ....

@fdintino
Copy link
Member

Oh interesting. Looks like this logic got added in Django 2.1. I'll see what I can do for a general fix.

@fdintino fdintino changed the title Image uploading in second level inline is ignored FileFields in deeply nested inlines broken in Django 2.1 Feb 1, 2019
@agalazis
Copy link

I think you need to reoppen, for I don't see it working I had to set in my parent form is_multipart method that returns true to make it work I could only see first level of inlines being checked from debugger

@agalazis
Copy link

also noticed that same goes with obj given to get_read_only_fields e.t.c. it is just first level inline object

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

3 participants