Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 944
Ria Avatar asked Feb 09 '26 17:02

Ria


2 Answers

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

It has been updated to work with Django 1.6

like image 71
s-block Avatar answered Feb 12 '26 07:02

s-block


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)
like image 33
Kasper Brandt Avatar answered Feb 12 '26 05:02

Kasper Brandt