4

I'm working on a web page which uses Django-quiz app. When you install the django-quiz, you can create quizes, questions etc. in Admin.

Unfortunately, there is no way, how to assign Quiz to my model Language so I'm looking for a way, how to add field Language into the model Quiz.

I've tried this but it does not work. I've tried already to create a proxy model with additional field but I realised that it is not possible in proxy models.

from quiz.models import Sitting,Quiz

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

Do you know what to do to add field to third party app model?

Milano
  • 18,048
  • 37
  • 153
  • 353
  • What kind of relationship is language supposed to have to a quiz? 1 (language) to many quizzes? – Sayse Apr 27 '16 at 11:04
  • For this time, OneToOne should be enough - for each language there will be one quiz. – Milano Apr 27 '16 at 11:05
  • 1
    Possible duplicate of [Django - how to extend 3rd party models without modifying](http://stackoverflow.com/questions/3433131/django-how-to-extend-3rd-party-models-without-modifying) – Sayse Apr 27 '16 at 11:06

2 Answers2

1

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

Since its one to one then you can just define the relationship on your own language class, django by default, will provide you the reverse lookup meaning

language_obj.quiz
quiz_obj.language

will both be valid.

Sayse
  • 42,633
  • 14
  • 77
  • 146
  • Oh, thanks, this is a very elegant solution. But I can't see the model choice 'Language' field in 'Quiz' field. How could I do that? I was thinking about changing admin class QuizInlineLanguage(admin.StackedInline): fields =[Quiz.language]; unregister(Quiz) register(Quiz,QuizInlineLnaugage) but it says that Quiz is not registered. – Milano Apr 27 '16 at 11:14
  • @Milano - I didn't understand your comment but it sounds as though you're talking about the models admin class which is a separate issue – Sayse Apr 27 '16 at 11:16
  • I wanted to add Language model into the Quiz model so I can assign a language to quiz. But I have to make it work in admin too because that's where the admin will create quizes. – Milano Apr 27 '16 at 11:18
  • Sorry for the sentence: "But I can't see the model choice 'Language' field in 'Quiz' field. " I meant: "I can't see the language field in Quiz form in admin" – Milano Apr 27 '16 at 11:20
0

Here is a relevant Django ticket, which was closed with a resolution of "wontfix" six years ago:

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

I think this comment provides some good information:

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 is now a standalone app, but it still makes itself relatively easy to customize. Here is the relevant documentation:

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

If a third party app doesn't make itself easy to customize, I would suggest asking the developer to update it and point them to the above links for examples on how to go about doing it.

Nick
  • 8,049
  • 18
  • 63
  • 107