Differences between Stacked inline and Tabular inline

时间:2017-11-13 06:28:49

标签: django django-models django-admin django-admin-actions

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.

1 个答案:

答案 0 :(得分:5)

我看到两个不同的问题:

  

我无法理解什么是内联和表格内联堆叠

基本上,两者都允许您在与父模型相同的页面上编辑模型。换句话说,有时候用户最好在编辑另一个模型时编辑某个模型,而不必在界面中的其他位置手动添加另一个实例。在您的特定情况下,您可以使用它来方便用户体验,允许用户同时添加链接到父项目的照片,而不必在您的管理表单之间不断更改。

现在,两者之间的区别非常简单: 布局 。实际上,两者在幕后完全相同,唯一的区别是用于渲染的模板。可以在源代码中看到here。因此,为您的项目选择一个只是关于界面布局的优先选择

  

我无法在管理员面板中看到这些模型

这可能是很多事情,但通常是因为您忘记使用makemigrationsmigrate来运行迁移。如果你这样做了,许多用户忘记的另一件事就是安装他们的应用程序。所以,在

<强> Setting.py

INSTALLED_APPS = ['Myproject.apps.Myapp']