Как отобразить данные одной модели в другой модели панели администратора django

У меня есть две модели: 1) SchoolProfile & 2) Content

Я хочу показать данные о содержимом в профиле школы, но данные должны отображаться в соответствии с иностранным ключом пользователя.

Иностранный ключ пользователя общий для обеих моделей.

Модель профиля школы

class SchoolProfile(models.Model):
    id=models.AutoField(primary_key=True)
    user=models.ForeignKey(User,on_delete=models.CASCADE,unique=True)
    school_name = models.CharField(max_length=255)
    school_logo=models.FileField(upload_to='media/', blank=True)
    incharge_name = models.CharField(max_length=255, blank=True)
    email = models.EmailField(max_length=255, blank=True)
    phone_num = models.CharField(max_length=255, blank=True)
    num_of_alu = models.CharField(max_length=255, blank=True)
    num_of_student = models.CharField(max_length=255, blank=True)
    num_of_cal_student = models.CharField(max_length=255, blank=True)

    def __str__(self):
        return self.school_name

Модель контента

class Content(models.Model):
    id=models.AutoField(primary_key=True)
    user=models.ForeignKey(User,on_delete=models.CASCADE)
    content_type = models.CharField(max_length=255, blank=True, null=True)
    show=models.ForeignKey(Show,on_delete=models.CASCADE, blank=True, null=True)
    sponsor_link=models.CharField(max_length=255, blank=True, null=True)
    status=models.BooleanField(default=False, blank=True, null=True)
    added_on=models.DateTimeField(auto_now_add=True)
    content_file=models.FileField(upload_to='media/', blank=True, null=True)
    title = models.CharField(max_length=255)
    subtitle = models.CharField(max_length=255, blank=True, null=True)
    description = models.CharField(max_length=500, blank=True, null=True)
    draft = models.BooleanField(default=False)
    publish_now = models.CharField(max_length=255, blank=True, null=True)
    schedule_release = models.DateField(null=True, blank=True)
    expiration = models.DateField(null=True, blank=True)
    tag = models.ManyToManyField(Tag, null=True, blank=True)
    topic = models.ManyToManyField(Topic, null=True, blank=True)
    category = models.ManyToManyField(Categorie, null=True, blank=True)
    
    def __str__(self):
        return self.title 

Я использовал Inline, но он показывает следующую ошибку:

<class 'colorcast_app.admin.SchoolProfileInline'>: (admin.E202) 'colorcast_app.SchoolProfile' has no ForeignKey to 'colorcast_app.SchoolProfile'.

Мой admin.py

class SchoolProfileInline(InlineActionsMixin, admin.TabularInline):
    model = SchoolProfile
    inline_actions = []

    def has_add_permission(self, request, obj=None):
        return False


class SchoolProfileAdmin(InlineActionsModelAdminMixin,
                  admin.ModelAdmin):
    inlines = [SchoolProfileInline]
    list_display = ('school_name',)

admin.site.register(SchoolProfile, SchoolProfileAdmin)

Использование StackedInline для связи двух моделей является прямой и гораздо более чистой практикой, так что в основном это мое решение ниже:

class ContentInlineAdmin(admin.StackedInline):
    model = Content

@admin.register(SchoolProfile)
class SchoolProfileAdmin(admin.ModelAdmin):
    list_display = ('school_name',)
    inlines = [ContentInlineAdmin]

 
Вернуться на верх