Django admin继承,在父模型中引用子模型id
Django admin inheritance, referencing child model id in parent model
我有一个基础模型和 2 个继承自基础模型的子模型
class Module(models.Model):
name = models.CharField(max_length=200, null=False)
def __str__(self):
return self.name
class A(Module):
title = models.CharField(max_length=300, null=False, verbose_name='Title')
image = models.FileField(upload_to='uploads/', null=True)
class B(Module):
title = models.CharField(max_length=300, null=False, verbose_name='Title')
sub_title = models.CharField(max_length=300, null=False, verbose_name='Title')
image = models.FileField(upload_to='uploads/', null=True)
这工作正常,Django 在引用父模型的子模型 table 中创建 table。
现在,我遇到的问题是有一个额外的应用程序有自己的模型,需要查询相关的父模型及其所有子模型。让我们假设这是我的应用程序引用模块 class
class Page(models.Model):
title = models.CharField(max_length=300, null=False)
slug = models.SlugField(max_length=300, null=False, db_index = True)
modules = models.ManyToManyField('modules.module')
通过当前的设置,Django 将父模型 ID 存储在子模型 table 中,我没有在客户端使用 Django 因此在我的 sql 查询中我想获取子模型模块附加到父级,通过引用子模型所引用的内容。请记住,Parent 仅链接到一个模型。
我查看了抽象代理模型以及 model_utils.managers InheritenceManager,但 none 将子模型信息存储在父模型中。
我该如何实现?
谢谢
关系已由 ManyToManyField
定义。能够显示这可能是您遇到的问题。
您可以参考 "through" 模型并像这样在管理员中注册它:
from django.contrib import admin
# https://docs.djangoproject.com/en/3.0/ref/contrib/admin/#inlinemodeladmin-objects
#
# You can use TabularInline, or StackedInline --- whichever meets your style preferences
#
class PageModuleInline(admin.TabularInline):
model = Page.modules.through # the implicit "join table" model
class PageAdmin(admin.ModelAdmin):
inlines = [
PageModuleInline,
]
class ModuleAdmin(admin.ModelAdmin):
inlines = [
PageModuleInline,
]
参见:https://docs.djangoproject.com/en/3.0/ref/contrib/admin/#working-with-many-to-many-models
我有一个基础模型和 2 个继承自基础模型的子模型
class Module(models.Model):
name = models.CharField(max_length=200, null=False)
def __str__(self):
return self.name
class A(Module):
title = models.CharField(max_length=300, null=False, verbose_name='Title')
image = models.FileField(upload_to='uploads/', null=True)
class B(Module):
title = models.CharField(max_length=300, null=False, verbose_name='Title')
sub_title = models.CharField(max_length=300, null=False, verbose_name='Title')
image = models.FileField(upload_to='uploads/', null=True)
这工作正常,Django 在引用父模型的子模型 table 中创建 table。
现在,我遇到的问题是有一个额外的应用程序有自己的模型,需要查询相关的父模型及其所有子模型。让我们假设这是我的应用程序引用模块 class
class Page(models.Model):
title = models.CharField(max_length=300, null=False)
slug = models.SlugField(max_length=300, null=False, db_index = True)
modules = models.ManyToManyField('modules.module')
通过当前的设置,Django 将父模型 ID 存储在子模型 table 中,我没有在客户端使用 Django 因此在我的 sql 查询中我想获取子模型模块附加到父级,通过引用子模型所引用的内容。请记住,Parent 仅链接到一个模型。
我查看了抽象代理模型以及 model_utils.managers InheritenceManager,但 none 将子模型信息存储在父模型中。
我该如何实现?
谢谢
关系已由 ManyToManyField
定义。能够显示这可能是您遇到的问题。
您可以参考 "through" 模型并像这样在管理员中注册它:
from django.contrib import admin
# https://docs.djangoproject.com/en/3.0/ref/contrib/admin/#inlinemodeladmin-objects
#
# You can use TabularInline, or StackedInline --- whichever meets your style preferences
#
class PageModuleInline(admin.TabularInline):
model = Page.modules.through # the implicit "join table" model
class PageAdmin(admin.ModelAdmin):
inlines = [
PageModuleInline,
]
class ModuleAdmin(admin.ModelAdmin):
inlines = [
PageModuleInline,
]
参见:https://docs.djangoproject.com/en/3.0/ref/contrib/admin/#working-with-many-to-many-models