Ria
Ria

Reputation: 225

Nested Inline in Django Admin

I have the following scenario where there are three models as follows which should be displayed nested in DJango admin. Am using Django 1.6 release and applied settings as put up in https://github.com/Soaa-/django-nested-inlines

However, it didnt turn up with the expected output. Is there any other solutions to implement nested inlines in Django. I'm a newbie to this framework. Kindly guide me to fix this issue.

model.py

class Project(models.Model):
    name = models.CharField(max_length=200)
    code = models.IntegerField(default=0)
    def __unicode__(self):
        return self.name

class Detail(models.Model):
    project = models.ForeignKey(Project)
    value = models.DecimalField(max_digits=5, decimal_places=2)
    location = models.IntegerField(default=0)

class Configuration(models.Model):
    detail = models.OneToOneField(Detail)
    content1 = models.IntegerField()
    content2 = models.IntegerField()

admin.py

from django.contrib import admin
from nested_inlines.admin import NestedModelAdmin, NestedTabularInline, NestedStackedInline

from myapp.models import Project, Detail, Configuration

class ConfigInline(NestedStackedInline):
    model = Configuration
    extra = 1

class DetailInline(NestedTabularInline):
    model = Detail
    inlines = [ConfigInline,]
    extra = 1

class ProjectAdmin(admin.ModelAdmin):
    inlines = [DetailInline]

admin.site.register(Project, ProjectAdmin)

Upvotes: 3

Views: 3068

Answers (3)

Vishal Shahu
Vishal Shahu

Reputation: 1

Before you import this below package first install this

pip install django-nested-admin then add this in installed app list

INSTALLED_APPS = [
   ...
   'nested_admin',
   ...
]

Now, you can import it.

from nested_inline.admin import NestedModelAdmin, NestedTabularInline, NestedStackedInline

Upvotes: 0

Kasper Brandt
Kasper Brandt

Reputation: 31

I believe you've forgotten to set the ProjectAdmin as a NestedModelAdmin:

admin.py :

from django.contrib import admin
from nested_inlines.admin import NestedModelAdmin, NestedTabularInline, NestedStackedInline

from myapp.models import Project, Detail, Configuration

class ConfigInline(NestedStackedInline):
    model = Configuration
    extra = 1

class DetailInline(NestedTabularInline):
    model = Detail
    inlines = [ConfigInline,]
    extra = 1

class ProjectAdmin(NestedModelAdmin):
    inlines = [DetailInline]

admin.site.register(Project, ProjectAdmin)

Upvotes: 1

s-block
s-block

Reputation: 318

try https://pypi.python.org/pypi/django-nested-inline .

It has been updated to work with Django 1.6

Upvotes: 1

Related Questions