使用:Django 1.8和Python 3.4
我有以下型号:
class SpaCenter(models.Model):
name = models.CharField(max_length=100)
website = models.CharField(max_length=200)
facebook_url = models.CharField(max_length=200)
faq = models.ManyToManyField(SpaCenterFAQ)
class SpaCenterFAQ(models.Model):
pass
class SpaCenterFAQLanguage(models.Model):
faq = models.ForeignKey(SpaCenterFAQ)
faq_language = models.CharField(max_length=2,
choices=LANGUAGE_CHOICES,
default=ENGLISH)
faq_question = models.CharField(max_length=200)
faq_answer = models.TextField()
总结:
使用Django内置管理界面我希望能够在SpaCenter管理界面中输入常见问题解答的所有不同翻译。
所以基本上这是一个嵌套关系,我知道这个帖子:
这两个社区构建的应用程序都试图解决这个问题:
但是,即使使用这些库,我仍然很难理解如何在Django Admin中定义它。
需要帮助了解如何在SpaCenter的管理界面中显示此类关系。
非常感谢。
编辑:
答案 0 :(得分:1)
使用djangosuperlines它应该看起来像:
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)
答案 1 :(得分: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)