django 1.5 admin inline extra
Asked Answered
T

5

14

I have some models with a large number of inlines. Since not all are needed for every object I declared the inline model with extra = 0 so that a small add + appeared on the inline.

However, this seems to no longer work in django 1.5. If extra = 0 is set, the inline is no longer editable.

Is there a way to get the 1.4 behavior into 1.5?

Example Code:

class ModelInline(admin.StackedInline):
    model = MyModel
    extra = 0

class OtherModelAdmin(admin.ModelAdmin)
    inlines = [ModelInline]

admin.site.register(OtherModel, OtherModelAdmin)

Edit (some screens):

Django 1.4: django 1.4 extra=0

Django 1.5: enter image description here

(Hinzufügen == add)

Teratoid answered 13/3, 2013 at 9:48 Comment(2)
Example code would be good to see here.Smyth
k, added some example codeTeratoid
N
8

This is already happen before.

The new javascript made this impossible because the "Add Another" button 
was controlled by max_num, and ignored a value of 0.
The javascript ignored a value of 0 because max_num has a default value of 0, 
and all the code using it had taken to equating max_num = 0 with being "off". 
So you can't actually have a maximum of 0. It's not possible.

There is a patch create by Gabrial Hurley to restores desired behaviour without breaking anything else. This is 3years ago and I don't know if it still working for Django 1.5. Just try :)

https://code.djangoproject.com/attachment/ticket/13023/13023_inlines_patch.diff

Here is the ticket for that same bug (3 years ago):

https://code.djangoproject.com/ticket/13023

Nematic answered 16/3, 2013 at 13:55 Comment(3)
I can't seem to apply this in 1.6 because total_form_count is no longer defined in models.py, only in formsets.py, and the code is different, so I'm not sure what to change. Can anyone help?Hemostat
@Hemostat In Django 1.6, it is called initial_form_countNematic
thanks, I found the equivalent lines and applied the patch, but my problem turned out to be something different...Hemostat
U
2

I ran into the same issue because I had the static admin content in a directory that was outside of django's install. Copying the Django 1.5 static content from django/contrib/admin/static/admin/js/ to STATIC_ROOT/admin/js fixed the issue.

Unlikelihood answered 18/3, 2013 at 16:51 Comment(1)
That's actually not a good thing to do. When you upgrade django version at some point, you'll run into trouble cause you won't see the new version of those static files (just had such an issue). If you server static content the proper way (docs.djangoproject.com/en/dev/howto/static-files) you should have no problemToulouse
U
0

the better solution would be to override the get_extra method of the inline itself. This works in Django 1.9, though i cannot say for earlier versions

Univalence answered 19/4, 2017 at 11:33 Comment(0)
D
0

All you have to do is add extra field.


For Example:

  1. models.py:

class Category(models.Model):
    name = models.CharField(max_length=50)
    def __str__(self):
        return self.name


class Product(models.Model):
    name = models.CharField(max_length=50)
    price = models.IntegerField()
    category = models.ForeignKey(Category, on_delete=models.CASCADE)

    def __str__(self):
        return '{}, {}'.format(self.name, self.company)

  1. admin.py:
    class InlinesProduct(admin.StackedInline):
        model = Product
        extra = 0     #<=== For remove empty fields from admin view

    @admin.register(Category)
    class CategoryAdmin(admin.ModelAdmin):
        inlines = [InlinesProduct]
Delocalize answered 3/4, 2021 at 11:22 Comment(0)
A
0

Use "get_extra()" as shown below if "extra" field doesn't work:

class ModelInline(admin.StackedInline):
    model = MyModel
    # extra = 0

    def get_extra(self, request, obj=None, **kwargs):
        return 0 # Is equivalent to "extra = 0"
    
class OtherModelAdmin(admin.ModelAdmin)
    inlines = [ModelInline]
    
admin.site.register(OtherModel, OtherModelAdmin)
Acariasis answered 21/6, 2022 at 17:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.