在djang的管理表单中使用多对多相关模型的字段作为内联

2024-09-27 21:35:01 发布

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

我面临着在管理表单中作为内联添加字段的问题。我的模型是如下所示:你知道吗

class Enrollment(BaseTableCore):
    iclass = models.ForeignKey(InstituteClass,blank=True,null=True,on_delete=models.SET_NULL )
    batch = models.ForeignKey(Batch,blank=True,null=True,on_delete=models.SET_NULL)
    icourse = models.ForeignKey(InstituteCourse,blank=True,null=True,on_delete=models.SET_NULL)
    enroll_num=models.CharField(max_length=20)**

class RegistrationProfile(models.Model):
    user = models.OneToOneField(User,related_name='userregistrationprofile')
    activeflag = models.BooleanField(blank=True) # This would be true when the student profile is activated by admin.
    usertype = models.IntegerField(choices=USER_TYPES)
    email = models.EmailField(null=True,blank=True)
    dob = models.DateField(blank=True,null=True)
    gender = models.IntegerField(choices=GENDER_CHOICE,null=True,blank=True)
    mobile_no = models.IntegerField(null=True,blank=True,help_text="Please Enter Your Mobile No : ")
    enrollment = models.ManyToManyField(Enrollment, through='ProfileEnrollment')
    courses_applied = models.ManyToManyField(InstituteCourse,through='AppliedForCourse')

class ProfileEnrollment(models.Model):
    profile = models.ForeignKey(RegistrationProfile)
    enrollment = models.ForeignKey(Enrollment)

class AppliedForCourse(models.Model):
    profile = models.ForeignKey(RegistrationProfile)
    courses = models.ForeignKey(InstituteCourse)
    batch = models.ForeignKey(Batch)

我遇到问题的管理表单如下如下所示:你知道吗

class AppliedForCourseInline(admin.TabularInline):
    model = AppliedForCourse
    fieldsets = ((None,{
        'fields':('courses','batch')}),
    )
    extra =0

class ProfileEnrollementInline(admin.TabularInline):
    model = ProfileEnrollment
    extra = 0

class RegistrationProfileAdmin(admin.ModelAdmin):
    form = RegistrationProfileAdminForm
    inlines = [AppliedForCourseInline,ProfileEnrollementInline]
    fieldsets = ((None,{
        'fields':('user','activeflag','usertype')}),
    )

AppliedForCourse的内联像往常一样工作,但麻烦是对ProfileEnrollmentInline的要求,因为我需要在内联中显示icourseiclassbatch&;enroll_num模型的Enrollment字段。你知道吗

我似乎想不出解决办法,如何着手解决这个问题?你知道吗


Tags: trueadminonmodelsbatchdeletenullclass

热门问题