Differences between Stacked inline and Tabular inline
Asked Answered
G

2

8

This is my models.py file
from django.db import models

# Create your models here.
class Item(models.Model):
    name=models.CharField(max_length=250)
    description = model.TextField()

    class Meta:
        oredering['name']
    def __unicode__(self):
        return self.name
    @permalink
    def get_absolute_url:
        retun ('item_detail',None,{'object_id':self_id})
class Photo(models.Model):
    item = models.ForiegnKey(Item)
    title=models.ChaField(max_length=250)

    image=models.IMageField(upload_to='photos')
    caption=models.TextField(blank=True)

    class Meta:
        ordering=['title']
    def __unicode__(self):
        return self.title
    @permalink
    def get_absolute_url(self):
        retun ('photo_detail',None,{'object_id':self_id})

And this is my admin.py :

from django.contrib import admin
from  models import Item
from  models import Photo

# Register your models here.
class PhotoInline(admin.StackedInline):
    model = Photo

class ItemAdmin(admin.ModelAdmin):
    inlines = [PhotoInline]

admin.site.register(Item, ItemAdmin)
admin.site.register(Photo)

But, I can't understand what is StackedInline and TabularInline, I referred to Django documentation but still couldn't understand what exactly it was.

Also, I can't see those models in my admin panel when I started the server, I don't understand why my models aren't registered on my admin page.

Grume answered 13/11, 2017 at 6:28 Comment(0)
D
18

The TabularInline displays data in table enter image description here

But StackedInline displays in row enter image description here

Dissatisfy answered 14/11, 2022 at 21:30 Comment(0)
N
11

I see two different questions:

I cant understand what is stacked inline and tabular inline

Basically, both allow you to edit models on the same page as a parent model. In other words, it is sometimes preferable for a user to have the possibility to edit a certain model while editing another one instead of having to manually add another instance somewhere else in your interface. In your particular case, you can use it to facilitate user experience by allowing the user to add photos linked to a parent item simultaneously whitout having to constantly change between your admin forms.

Now, the difference between the two is really simple to understand: Layout. Indeed, both works exactly the same behind the scenes, the only difference is the template used for rendering. It can be seen here in the source code. So, picking one for your project is only a matter of preference regarding the interface layout

I cant see those models in my admin panel

This can be a lot of things, but often it's because you forgot to run your migrations with makemigrations and migrate. If you did, another thing that a lot of users forget is to install their app. So, in

Setting.py

INSTALLED_APPS = ['Myproject.apps.Myapp']
Nabal answered 13/11, 2017 at 14:3 Comment(1)
the problem was i didnt add that to my settings.py./installed_apps and thanks again for explaining me in detailGrume

© 2022 - 2024 — McMap. All rights reserved.