简体   繁体   English

django从模型字段获取值

[英]django get values from a model field

I have model : 我有模特:

class Question(models.Model):
    user = models.ForeignKey(User)
    title = models.CharField(max_length=120)
    description = models.TextField()
    category = models.ForeignKey('Category')
    answers = models.ManyToManyField('Answer',related_name='answer_name', blank=True)
    post_date = models.DateTimeField(auto_now=True)
    published = models.BooleanField(default=False)
    vote = models.IntegerField(default=0) # Field that denotes the no of vote for particular question

    def __unicode__(self):
        return self.title

And my view: 我的看法是:

def VoteCountView(request, pk):

    ques = Question.objects.get(id=pk)
    cpk = ques.category.id
    valid_user = ques.user.username

    if request.user.is_general_user:
        if request.user.username == valid_user:
            messages.warning(request, "You cannot vote your own question")
            return redirect("question-detail", pk, cpk)
        else:
            vote_limit = Question.objects.values_list("vote", flat=True).filter(pk=pk).filter(user=request.user)#[0][0]
            print "vote count"
            print vote_limit
            if vote_limit < 1:
                vote_count = ques
                print vote_count.user
                count = vote_count.vote
                count +=1
                vote_count.vote = count
                vote_count.user = request.user
                print vote_count.user
                vote_count.save()
            else:
                messages.warning(request,"You cannot vote twice")
                return redirect("question-detail", pk, cpk)
    else:
        messages.warning(request,"You are not allowed to vote a question")
        return redirect("question-detail", pk, cpk)

Here what I want is other user can vote the question except the user who posted the question. 在这里我想要的是其他用户可以投票给该问题,但发布该问题的用户除外。 When other user vote the question it says list index out of range. 当其他用户投票该问题时,它说列表索引超出范围。 Can I achieve this from this model or I have to make separate model for vote. 我可以从此模型中实现此目标,还是必须单独制定投票模型。

Also if the user that are not logged in or not general use, if the vote it says user object has no attribute is_general_user?? 同样,如果未登录的用户或未正常使用的用户,则如果投票显示该用户对象没有属性is_general_user?

Can anyone help me on this ??? 谁可以帮我这个事 ???

couple of things to optimize: 需要优化的几件事:

instead of 代替

ques = Question.objects.get(id=pk)

better use get_object_or_404(Question, id=pk) . 最好使用get_object_or_404(Question, id=pk) you never know what happens in future... 你永远不知道将来会发生什么...

before you do if request.user.is_general_user: you need to check if user is authenticated: 在执行if request.user.is_general_user:之前,您需要检查用户是否已通过身份验证:

if request.user.is_authenticated():
    if request.user.is_general_user: 
        #....

this way, you make sure that you are playing with Authenticated User and not with Anonymous User (the one how is not logged in). 这样,您可以确保与身份验证用户一起玩,而不是与匿名用户一起玩(不登录的方式)。 And this may solve your other list index out of range problem, because now request.user in your filter is an authencated one who is also in db. 这可能会解决您的其他list index out of range问题,因为现在过滤器中的request.user是经过身份验证的用户,该用户也在db中。

you can write your query also this cleaner way: 您还可以使用以下更简洁的方式编写查询:

vote_limit = request.user.question_set.values_list("vote", flat=True).filter(pk=pk)#[0][0]

i dont know what you mean by [0][0] , if want to get the latest Question, you can do: 我不知道您的意思是[0][0] ,如果要获取最新的问题,可以执行以下操作:

.order_by('-id')[0]

instead of [0][0] 而不是[0][0]

UPDATE : i didnot go into what you really want into context. 更新 :我没有进入您真正想要的上下文。 I just tried to fix the bugs. 我只是试图修复错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM