2017-06-12 71 views
0

這是數據庫模型,用戶更改重要性,並根據用戶選擇的級別,將得分/點分配爲 ,但是無論選擇什麼,它顯示的所有值都是my_points的值爲300,而他們的值爲0。爲什麼我的數據沒有被更新?

from django.db import models 
from django.conf import settings 
from django.db.models.signals import post_save 
# Create your models here. 
class Question(models.Model): 
    text = models.TextField() 
    active = models.BooleanField(default = True) 
    draft = models.BooleanField(default = False) 
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False) 
    #answers = models.ManyToManyField('Answer') 

    def __unicode__(self): 
     return self.text[:10] 


class Answer(models.Model): 
    question = models.ForeignKey(Question) 
    text = models.CharField(max_length=120) 
    active = models.BooleanField(default=True) 
    draft = models.BooleanField(default=False) 
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False) 


    def __unicode__(self): #def __str__(self): 
     return self.text[:10] 
LEVELS = (
    ('Mandatory', 'Mandatory'), 
    ('Very Important', 'Very Important'), 
    ('Somewhat Important', 'Somewhat Important'), 
    ('Not Important', 'Not Important'), 
    ) 

class UserAnswer(models.Model): 
    user = models.ForeignKey(settings.AUTH_USER_MODEL) 
    question = models.ForeignKey(Question) 
    my_answer = models.ForeignKey(Answer, related_name = 'user_answer') 
    my_answer_importance = models.CharField(max_length=50, choices= LEVELS) 
    my_points = models.IntegerField(default=-1) 
    their_answer = models.ForeignKey(Answer, null=True, blank=True, related_name = 'match_answer') 
    their_importance = models.CharField(max_length=50, choices= LEVELS) 
    their_points = models.IntegerField(default=-1) 
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False) 

    def __unicode__(self): 
     return self.my_answer.text[:10] 

def score_importance(importance_level): 
    if importance_level == "Mandatory": 
     points = 300 
    elif importance_level == "Very Important": 
     points = 200 
    elif importance_level == "Somewhat Important": 
     points = 50 
    elif importance_level == "Not Important": 
     points = 0 
    else: 
     points = 0 
    return points 


def update_user_answer_score(sender, instance, created, *args, **kwargs): 
    #print sender 
    print instance 
    #print created 
    if instance.my_points == -1: 
     my_points = score_importance(instance.my_answer_importance) 
     instance.my_points = my_points 
     print my_points 
     instance.save() 
    if instance.their_points == -1: 
     their_points = score_importance(instance.their_importance) 
     instance.their_points = their_points 
     print my_points 
     instance.save() 


post_save.connect(update_user_answer_score, sender=UserAnswer) 
+0

你爲什麼這樣做?由於點總是取決於重要性級別,爲什麼不有一個整數字段的文本作爲描述和點作爲價值? –

回答

1

你的代碼是不必要的複雜。你真的不需要分開的my_pointsmy_answer_importance字段和你使用選擇的方式,這種方式可以打敗使用選擇的整個觀點。我建議你改變你的代碼是這樣的:

LEVELS = (
    (300, 'Mandatory'), 
    (200, 'Very Important'), 
    (50, 'Somewhat Important'), 
    (0, 'Not Important'), 
    ) 

class UserAnswer(models.Model): 
    user = models.ForeignKey(settings.AUTH_USER_MODEL) 
    question = models.ForeignKey(Question) 
    my_answer = models.ForeignKey(Answer, related_name = 'user_answer') 
    my_points = models.IntegerField(default=-1, choices=LEVELS) 
    their_answer = models.ForeignKey(Answer, null=True, blank=True, related_name = 'match_answer') 
    their_points = models.IntegerField(default=-1, choices=LEVELS) 
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False) 

    def __unicode__(self): 
     return self.my_answer.text[:10] 

現在你不需要你的信號和整個代碼塊可以被刪除。

+0

我需要將它保存到我的數據庫,並按照教程,所以按照它。我的上面的代碼有什麼問題嗎? – Raghu

+0

是的,這就是我在答案中所說的。並非所有的教程都是平等的。有些教程是絕對垃圾。 – e4c5

+0

我正在爲用戶創建一個選擇,以選擇他/她的重要性等級以及她想要匹配的人員/他的等級。你的代碼也給了我錯誤。請看這個https://github.com/codingforentrepreneurs/matchmaker-2/blob/bb3eac029ac2b5fbb7c880c6d1f1dada2decd23d/src/questions/models.py – Raghu

相關問題