无法在 Django 视图中创建对象

Can not create object in Django views

我想在视图中创建对象,但不明白为什么我会收到 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)

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

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