django-admin-sortable2 icon indicating copy to clipboard operation
django-admin-sortable2 copied to clipboard

Admin save fails because of TabularInline when extra > 1

Open dekked opened this issue 8 years ago • 9 comments

I want to inline MyInlineModel (which has a required FileField):

class MyInline(SortableInlineAdminMixin, admin.TabularInline):
    model = MyInlineModel
    extra = 3

I do this, and I open one of the entries in my DB at random. Then I cannot save the exact same object, because I get "This field is required" in the BLANK entries added because of the extra option (these rows don't get the "draggable" area, of course).

If I set extra to 0, then the same save works, but unfortunately if I make changes reordering the inlines, they don't get saved for some reason :(

If I stop inheriting from SortableInlineAdminMixin then I can open and entry and hit save, without issues.

I am using Django 1.10.4.

dekked avatar Feb 14 '17 04:02 dekked

Could you please retry with Django-1.9. I just want to ensure, that it's not related to a template issue.

jrief avatar Feb 14 '17 08:02 jrief

Hey @jrief! Just tried with Django 1.9.12 and I get the exact same behavior.

dekked avatar Feb 14 '17 11:02 dekked

Could you please dump MyInlineModel

jrief avatar Feb 14 '17 14:02 jrief

It would be something like:

class MyInlineModel(models.Model):
    parent = models.ForeignKey(Parent, related_name='parent')
    order = models.PositiveIntegerField(default=0, editable=False, db_index=True)
    image = ImageField(upload_to='path', height_field='height', width_field='width')
    height = models.PositiveIntegerField(null=True, blank=True, editable=False, default=0)
    width = models.PositiveIntegerField(null=True, blank=True, editable=False, default=0)

    class Meta:
        ordering = ['order']

dekked avatar Feb 14 '17 14:02 dekked

I would suggest to do the following:

in models.py:

class ParentImage(models.Model):
    image = image.FilerImageField()
    parent = models.ForeignKey('myapp.Parent')
    order = models.PositiveIntegerField(default=0, blank=False, null=False)

    class Meta:
        ordering = ['order']


class Parent(models.Model):
    # other fields
    images = models.ManyToManyField('filer.Image', through=ParentImage)

in admin.py:

class MyInline(SortableInlineAdminMixin, admin.TabularInline):
    model = MyInlineModel
    extra = 1
    ordering = ['order']

@admin.register(Parent)
class ParentAdmin(admin.ModelAdmin):
    inlines = [MyInline]

jrief avatar Feb 14 '17 15:02 jrief

@jrief is there any way to fix this with the current models? It will take some time to do the necessary migrations to test this.

Any reason why just using ForeignKey does not work? This used to work with django-admin-sortable (which I am moving from).

dekked avatar Feb 14 '17 15:02 dekked

I would have to dig deeply into that problem, but I assume it has to do with some django-filer internals.

Anyway, what you try to achieve is a sortable M2M-relation from model Parent with a set of images. Therefore this approach is more appropriate anyway, and btw. what I use in my projects.

jrief avatar Feb 14 '17 16:02 jrief

I don't use django-filer. I use the vanilla Django ImageField.

This is why I find it weird. This case should probably work just fine, as it does in django-admin-sortable.

dekked avatar Feb 14 '17 17:02 dekked

ups, oversaw that. Well then it's even more weird, because the internal file field does not contain any foreign keys.

jrief avatar Feb 14 '17 17:02 jrief