small improvement to admin panel (userAnswer is now in user)

This commit is contained in:
tkupek 2016-02-09 21:46:52 +01:00
parent 4a1716de73
commit a6589b9e84
4 changed files with 16 additions and 7 deletions

Binary file not shown.

View file

@ -12,7 +12,16 @@ class QuestionAdmin(admin.ModelAdmin):
OptionInline,
]
class UserAnswerInline(admin.TabularInline):
model = UserAnswer
class UserAdmin(admin.ModelAdmin):
inlines = [
UserAnswerInline,
]
admin.site.register(Question, QuestionAdmin)
admin.site.register(Setting),
admin.site.register(UserAnswer),
admin.site.register(User)
admin.site.register(User, UserAdmin)

View file

@ -46,11 +46,11 @@ class User(models.Model):
class UserAnswer(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=False)
questionId = models.ForeignKey(Question, on_delete=models.CASCADE, null=False)
question = models.ForeignKey(Question, on_delete=models.CASCADE, null=False)
answers = models.CharField(max_length=100, null=True)
def __str__(self):
return str(self.user) + " - " + str(self.questionId)
return str(self.user) + " - " + str(self.question)
class Meta:
unique_together = (('user', 'questionId'),)
unique_together = (('user', 'question'),)

View file

@ -31,13 +31,13 @@ def get_answer(request):
user = User.objects.get(token=request_token)
try:
user_answer = UserAnswer.objects.get(questionId=question.id, user=user.id)
user_answer = UserAnswer.objects.get(question=question.id, user=user.id)
except ObjectDoesNotExist:
user_answer = None
if user_answer is None:
user_answer = UserAnswer()
user_answer.questionId = question
user_answer.question = question
user_answer.user = user
user_answer.answers = request_answers
user_answer.save()