[Fixed]-Nested ManyToMany Field in Django

1👍

using djangosuperlines it should look like:

from django.contrib.admin import TabularInline, StackedInline, site
from super_inlines.admin import SuperInlineModelAdmin, SuperModelAdmin

from .models import *


class SpaCenterFAQLanguageInlineAdmin(SuperInlineModelAdmin, TabularInline):
    model = SpaCenterFAQLanguage


class SpaCenterFAQInlineAdmin(SuperInlineModelAdmin, StackedInline):
    model = SpaCenter
    inlines = (SpaCenterFAQLanguageInlineAdmin,)


class SpaCenterAdmin(SuperModelAdmin):
    inlines = (SpaCenterFAQInlineAdmin,)


site.register(SpaCenter, SpaCenterAdmin)

0👍

from django.db import models

class SpaCenter(models.Model):
    name = models.CharField(max_length=100)
    website = models.CharField(max_length=200)
    facebook_url = models.CharField(max_length=200)
    faqs = models.ManyToManyField('SpaCenterFAQ', blank=True)


class SpaCenterFAQ(models.Model):
    spa_centers = models.ManyToManyField('SpaCenter', blank=True)

class SpaCenterFAQLanguage(models.Model):
    ENGLISH = '1'
    LANGUAGE_CHOICES = ((ENGLISH, 'english'),)

    faq = models.ForeignKey(SpaCenterFAQ)
    faq_question = models.CharField(max_length=200)
    faq_answer = models.TextField()
    faq_language = models.CharField(max_length=2,
                        choices=LANGUAGE_CHOICES,
                        default=ENGLISH


from django.contrib import admin

from .models import *


class SpaCenterFAQLanguageInlineAdmin(admin.TabularInline):
    model = SpaCenterFAQLanguage

class SpaCenterFaqAdmin(admin.ModelAdmin):
    inlines = SpaCenterFAQLanguageInlineAdmin,


class SpaCenterAdmin(admin.ModelAdmin):
    pass

admin.site.register(SpaCenter, SpaCenterAdmin)
admin.site.register(SpaCenterFAQ, SpaCenterFaqAdmin)

Leave a comment