2

This snippet is from djangoproject.com tutorial. Can someone explain why this line is written in this way:

inlines =[ChoiceInline] 

instead of

inlines = ChoiceInline

why there are square brackets around class ChoiceInline?


from django.contrib import admin
from naslovnica.models import Poll
from naslovnica.models import Choice

class ChoiceInline(admin.TabularInline):
    model = Choice
    extra = 3

class PollAdmin(admin.ModelAdmin):
    fieldsets = [
    (None,               {'fields': ['question']}),
    ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
    ]
    inlines =[ChoiceInline]
    list_display = ('question', 'pub_date', 'was_published_recently')
    search_fields = ['question']
    list_filter = ['pub_date']

admin.site.register(Poll, PollAdmin)
1
  • Thank you all for answers. I choosed Lafad just because im new to programming and his answer was easiest for me to undersood. But all of answers are meaningfull for me now. Commented Mar 10, 2014 at 15:10

4 Answers 4

4

It's not a "class call". It's simply a list. You can have multiple inlines, hence the plural, and you need to have them inside a list or a tuple.

Sign up to request clarification or add additional context in comments.

4 Comments

what is exactly inline? why it is written in class? do you know more about that? where i can read more about it?
But that is explained in exactly the place where you got that code from.
You could also have inlines =(ChoiceInline,). Where (ChoiceInline,) is a tuple of only one element.
@Daniel I am trying to figure out about what is inlines part of and i couldnt do that from that tutorial. so i asked for help here.
1

inlines =[ChoiceInline] this is not a calling of class. This will create new list which has class as first element.

Class calling will be done as ChoiceInline().

2 Comments

can you describe more what inlines is and can there be more elements and also which kind? other elements are classes too?
inline is list and ChoiceInline is class. When you declare inlines =[ChoiceInline] it will define inline as list with one element. inline[0] will return you ChiceInline class, not ChoiceInline object.
1

Inlines are a list of InlineModelAdmin, TabularInline or StackedInline. This allows you to add "inline" forms for related models.

https://docs.djangoproject.com/en/dev/ref/contrib/admin/#inlinemodeladmin-objects

The admin interface has the ability to edit models on the same page as a parent model. These are called inlines.

Comments

1

It receives a list or a tuple of Classes, because Django uses it as a special ModelAdmin class, like an extension to BaseModelAdmin.

Internally, Django will instance this classes, as you can see here:

def get_inline_instances(self, request, obj=None):
    inline_instances = []
    for inline_class in self.inlines:
        inline = inline_class(self.model, self.admin_site)
        if request:
            if not (inline.has_add_permission(request) or
                    inline.has_change_permission(request, obj) or
                    inline.has_delete_permission(request, obj)):
                continue
            if not inline.has_add_permission(request):
                inline.max_num = 0
        inline_instances.append(inline)

    return inline_instances

For a better understand I suggest you to see the options file in django.contrib.admin : https://github.com/django/django/blob/master/django/contrib/admin/options.py

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.