I modelling a quiz and the associated questions as follows:
# models
class Question(models.Model):
title = models.TextField()
category = models.TextField()
class Quiz(models.Model):
questions = models.ManyToManyField(Question, through='OrderedQuestion')
class OrderedQuestion(models.Model):
# A through table to allow ordering of questions
question = models.ForeignKey(Question, ...)
quiz = models.ForeignKey(Quiz, ...)
order = models.PositiveIntegerField(default=0)
I have two types of questions that are handled by proxy models:
# proxy models to handle specific question categories
class BoatQuestion(Question):
objects = BoatQuestionManager() # handles setting category
class Meta:
proxy = True
and a similar one for CarQuestion
.
I want to be able to edit BoatQuestions
and CarQuestions
independently from each other but on the same admin page. The admin setup is:
class BoatQuestionInline(admin.TabularInline):
model = BoatQuestion.quiz.through
class CarQuestionInline(admin.TabularInline):
model = CarQuestion.quiz.through
class QuizAdmin(admin.ModelAdmin):
model = Quiz
inlines = (BoatQuestionInline, CarQuestionInline)
but whenever I change the questions in the boat question section, the questions in the car section update to match it and vice versa.
Is there any way to show these on the same admin page but change them independently?
The problem lies in your inlines. You use the same model for both, which is fine. But as you want to show only certain Question
s, you have to adjust the QuerySet
for each inline and add an appropriate .filter()
. (I am guessing here, how you distinguish the category of the questions.)
class BoatQuestionInline(admin.TabularInline):
model = BoatQuestion.quiz.through
def get_queryset(self, *args, **kwargs):
return OrderedQuestion.objects.filter(question__category='boat')
class CarQuestionInline(admin.TabularInline):
model = CarQuestion.quiz.through
def get_queryset(self, *args, **kwargs):
return OrderedQuestion.objects.filter(question__category='car')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.