无法在Django视图中创建对象

时间:2016-03-02 11:12:27

标签: python django django-views django-orm

我想在视图中创建对象,但无法弄清楚为什么会出现500错误。

以下是相关代码:

lvt = LastVisitedTopic.objects.create(user=uid, topic=t.id, lastvisited=lv)

模型是:

class LastVisitedTopic(models.Model):
    user = models.ForeignKey(User)
    topic = models.ForeignKey(Topic)
    lastvisited = models.DateTimeField(auto_now=True)
    class Meta:
    managed = True     
    app_label = 'myforum'

传递给create方法的参数似乎没问题:

print 'uid, t.id, lv \n', uid, t.id, lv

收率:

    uid, t.id, lv 
1 202798 2014-10-19 03:10:00+00:00

我对此持有一段时间,所以非常感谢你的线索。

更新 这是整个观点,万一它可以帮助:

def notify_ajax(request):
    #if request.method == 'GET':
        args = {}
        alerts = []
        mt_alerts = []
        uid = request.user.id
        numalerts = 0

        if Topic.objects.filter(Q(post__creator_id=uid) | Q(creator_id=uid)).exists():
            mypartopics = Topic.objects.filter(Q(post__creator_id=uid) | Q(creator_id=uid)).distinct().order_by("-created")
            for t in mypartopics:
                print t.title
                #Check whether there is record of the topic in LastVisitedTopic
                if not LastVisitedTopic.objects.filter(topic_id=t.id, user_id=uid).exists():
                    print 'LastVisitedTopic not exists so to be created...'
                    #Calculate the record
                    #If topic has posts by this user
                    if Post.objects.filter(topic_id=t.id, creator_id=uid).exists():
                        print 'this topic has posts...\n'
                    #Last post time by this user is assumed as the last time he visited the topic
                        last_post = Post.objects.filter(topic_id=t.id, creator_id=uid).latest('created')
                        print 'latest post found'
                        lv = last_post.created
                        print 'lv to be added from post', lv
                        print 'uid, t.id, lv \n', uid, t.id, lv
                        lvt = LastVisitedTopic.objects.create(user=uid, topic=t.id, lastvisited=lv)
                        print 'last visit record created'                   
                    #Else last visit is assumed as the time when the topic is created
                    else:
                        lv = t.created
                        print 'lv added from topic'


                        lvt = LastVisitedTopic.objects.create(user=uid,topic=t.id, lastvisited='%s')


                        print 'last visit record created equal to topic creation time'                  



                else:
                    print 'record for this topic exists', 
                    track = LastVisitedTopic.objects.get(topic_id=t.id, user_id=uid)
                    print 'last visited at', track.lastvisited

                #Check whether the topic lastposted field is not empty, if so, fill it
                if not t.lastposted:
                    if Post.objects.filter(topic_id=t.id).exists():
                        topic_lp= Post.objects.filter(topic_id=t.id).latest('created')
                        t.lastposted = topic_lp.created
                        t.save()
                        print 'new t.lastposted saved'
                    else:
                        t.lastposted = t.created
                        t.save()
                else:
                    print 't.lastposted for this topic exists', t.lastposted




                #Now compare last visit by the user with last topic post
                try:
                    print '\n\n\nnow trying...'
                    track = LastVisitedTopic.objects.get(topic_id=t.id, user_id=uid)
                    print 'track', track.id
                    last_visit = track.lastvisited
                    print 'last visit', last_visit
                    print 't.lastposted', t.lastposted

                    if t.lastposted > last_visit :
                        print 'topic posted after the last visit'
                        #last_posts = Post.objects.filter(topic_id=t.id).latest()
                        #if last_post.created >= last_visit:
                        alerts.append({'title':t.title, 'slug':t.slug}) 
                    else:
                        print 'lastposted before last visit'
                except:
                    print 't excpeton'
                    pass

            print 'alerts', alerts
            print 'alerst length', len(alerts)
            args['alerts'] = alerts
            return render(request, '_alerts.html', args)

1 个答案:

答案 0 :(得分:1)

就像错误所说,你需要一个真实的用户,而不是一个ID。您可以直接传递request.user。你的主题可能会得到同样的错误;再次,你应该只传递对象本身。

LastVisitedTopic.objects.create(user=request.user, topic=t, lastvisited=lv)