Django管理通用内容类型多模型内联表单

2024-05-15 03:15:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚开始使用Django,我有点纠结于多模型字段,也就是泛型关系(Content-Type)

我有一个通用的内容类型“学生解决方案”,可以属于:

  • 一个Org模型
  • 一个Institution模型
  • 一个Campus模型

因此,在这3个模型中,我有一个相反的关系,如下所示:

# Reverse generic relation - XXX See https://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/#reverse-generic-relations
student_solutions = GenericRelation('student_solution.StudentSolution')

I'm not sure whether this is the right approach, I think so, but a confirmation is welcome :)


它现在工作得很好,但在Django管理UI中并不友好,请看它在Django Admin上如何显示,当创建学生解决方案时(我希望选择框显示一个label字段,而不是手动输入内容类型ID):

django admin create entity

当创建一个组织、机构或校园时,Django管理中根本不显示该字段(因此我可能配置错误)

我尝试遵循How to replace content_type and object_id fields by a field with actual object in admin inline?来改进UI,允许使用对象的标签选择正确的内容类型和“对象”。但现在不行了。在


student_solution/models.py

^{pr2}$

student_solution/admin.py

from django.contrib import admin
from modeltranslation.admin import TranslationAdmin

from tfp_backoffice.apps.org.models import Org
from tfp_backoffice.apps.student_solution.forms import StudentSolutionAdminForm, GenericStudentSolutionOwnerChoicesFieldForm
from tfp_backoffice.apps.student_solution.models import StudentSolution


class StudentSolutionInlineAdmin(admin.TabularInline):
  form = GenericStudentSolutionOwnerChoicesFieldForm
  model = Org  # TODO not sure at all about that, should be either of 3 related ContentTypes (Org | Institution | Campus)
  # This throw error "<class 'tfp_backoffice.apps.student_solution.admin.StudentSolutionInlineAdmin'>: (admin.E202) 'org.Org' has no ForeignKey to 'student_solution.StudentSolution'."


class StudentSolutionAdmin(TranslationAdmin):
  form = StudentSolutionAdminForm
  inlines = [
    StudentSolutionInlineAdmin,
  ]


admin.site.register(StudentSolution, StudentSolutionAdmin)

student_solution/forms.py

from django import forms
from django.contrib.contenttypes.models import ContentType

from tfp_backoffice.apps.org.models import Org
from tfp_backoffice.apps.student_solution.models import CONTENT_TYPE_CHOICES, StudentSolution


class StudentSolutionAdminForm(forms.ModelForm):
  class Meta:
    model = StudentSolution
    fields = '__all__'  # Keep all fields    

class GenericStudentSolutionOwnerChoicesFieldForm(forms.ModelForm):
  ct_place_type = ContentType.objects.get_for_model(Org)  # TODO not sure at all about that, should be either of 3 related ContentTypes (Org | Institution | Campus)

  object_id = forms.ModelChoiceField(
    Org.objects.all(),
    limit_choices_to=CONTENT_TYPE_CHOICES,
    label='Student solution'
  )
  content_type = forms.ModelChoiceField(
    ContentType.objects.all(),
    initial=ct_place_type,  
    limit_choices_to=CONTENT_TYPE_CHOICES,  # should I use this here?
    widget=forms.HiddenInput()
  )

  def clean_object_id(self):
    return self.cleaned_data['object_id'].pk

  def clean_content_type(self):
    return self.ct_place_type

但是这段代码不工作,在启动服务器时抛出这个错误

django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:

<class 'tfp_backoffice.apps.student_solution.admin.StudentSolutionInlineAdmin'>: (admin.E202) 'org.Org' has no ForeignKey to 'student_solution.StudentSolution'.


Tags: appsfromorgimportadminmodelstypeforms
2条回答

如果我理解您想要正确地做什么,您应该在OrgInstitution、和{}管理员中的{},并且应该是GenericTabularInlinehttps://docs.djangoproject.com/en/2.2/ref/contrib/contenttypes/#generic-relations-in-admin)。在

例如,{cd6>中的某样东西:

from django.contrib import admin
from django.contrib.contenttypes.admin import GenericTabularInline
from django import forms

from .models import Org
from student_solution.models import StudentSolution

class StudentSolutionInlineAdmin(GenericTabularInline):
    model = StudentSolution
    extra = 1

class StudentSolutionAdminForm(forms.ModelForm):
    class Meta:
        model = StudentSolution
        fields = '__all__'  # Keep all fields 

@admin.register(Org)
class OrgAdmin(admin.ModelAdmin):
    form = StudentSolutionAdminForm
    inlines = [StudentSolutionInlineAdmin]

这将允许您从Org管理员中添加与Org相关的StudentSolution。在

你可以看看这个:https://docs.djangoproject.com/en/2.2/ref/contrib/contenttypes/#generic-relations-in-admin

如果您使用的是内容类型功能框架,则它们有特殊的内联类型可供使用

相关问题 更多 >

    热门问题