在django中使用多个相关模型的字段作为内联在admin表单中

时间:2012-12-06 06:18:11

标签: python django-admin

我遇到了在管理表单中添加字段作为内联的问题。我的模型如下: -

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个字段。

我似乎无法找出解决方案,如何解决这个问题?

0 个答案:

没有答案