如何向第三方应用程序模型添加字段?

How to add fields to third party app model?

我正在开发一个使用 Django-quiz 应用程序的网页。安装 django-quiz 后,您可以在 Admin 中创建测验、问题等。

不幸的是,没有办法,如何将 Quiz 分配给我的模型 Language 所以我正在寻找一种方法,如何将字段 Language 添加到模型中 Quiz.

我已经试过了,但没有用。我已经尝试创建一个带有附加字段的代理模型,但我意识到这在代理模型中是不可能的。

from quiz.models import Sitting,Quiz

class QuizAddLanguage(models.Model):
    quiz = models.OneToOneField(Quiz)
    language = models.ForeignKey(Language)

你知道如何将字段添加到第三方应用程序模型吗?

For this time, OneToOne should be enough - for each language there will be one quiz

因为它是一对一的,所以你可以用你自己的语言定义关系class,django 默认情况下,会为你提供反向查找含义

language_obj.quiz
quiz_obj.language

两者都有效。

这是一张相关的 Django 工单,它在六年前以 "wontfix" 的分辨率关闭:

https://code.djangoproject.com/ticket/14969

我认为this comment提供了一些很好的信息:

Comments gives you the *right* way to handle this problem -- you define an interface, and make the model itself pluggable. Not all Django's contrib apps follow this approach, but that doesn't mean we bake monkeypatching into the core -- we fix the contrib apps.

django.contrib.comments 现在是一个独立的应用程序,但它仍然相对容易定制。这是相关文档:

https://django-contrib-comments.readthedocs.io/en/latest/custom.html

如果第三方应用无法轻松自定义,我建议让开发人员对其进行更新,并将他们指向上面的链接,以获取有关如何进行更新的示例。