Django模型只有在查询后才能保存

时间:2016-03-01 20:21:13

标签: python django django-models django-views django-queryset

我已经在Django工作了一段时间,现在我遇到了这个奇怪的问题。

我在流媒体网站上添加了评分功能,为此我编写了一个通过网址调用的视图:

url(r'^channel/rate/(?P<stream_id>[\d]*)/(?P<rate>[\d]*)/?$', 'eros.streaming.views.view_rate_channel', 
   name='view_rate_channel'),

def view_rate_channel(request,stream_id,rate):
    if(stream_id and rate and request.user.is_authenticated()):
        stream= Stream.objects.get(id=stream_id)
        if stream not in request.user.channel.rated_streams.all():
            if stream.channel.user != request.user:
                channel=Channel.objects.get(stream=stream)
                channel.rating= channel.rating+int(rate)    
                print("channel rating:   "+str(channel.rating))
                #this prints fine in any case
                channel.n_voters = channel.n_voters +1
                channel.save()
                stream.rating= stream.rating+int(rate)
                stream.n_voters= stream.n_voters+1
                stream.save()    
                request.user.channel.rated_streams.add(stream)
                request.user.channel.save()
                return HttpResponseRedirect('/channel/'+str(stream.channel.id)+'/')  
    return HttpResponseRedirect('/channel/'+str(stream.channel.id)+'/')

当我检查数据库时,我没有保存在channel.save()之后在Channel对象上所做的更改,只有Stream对象的更改,我发现这些更改很奇怪。

因此,在做了一些调试之后,我决定对此进行评论,如果我使用这样,那么用户就不能对流进行多次评分:

        ##if stream not in request.user.channel.rated_streams.all():

现在channel.save()正在运行!糟糕的是,我不能让用户多次评价一个流。

这是模型的简化版本:

class Channel(models.Model):
    user = models.OneToOneField(User)
    rating = models.IntegerField(default=0)
    n_voters = models.IntegerField(default=0)
    rated_streams = models.ManyToManyField('Stream', related_name="rated_streams")
    description = models.TextField(default="")

class Stream(models.Model):
    ## I think that maybe this relation is what is causing me trouble (?):
    channel = models.ForeignKey(Channel)
    rating = models.IntegerField(default=0)
    n_voters = models.IntegerField(default=0)

是否有任何不良做法或错误的查询,我这样做是为了实现这一目标?非常感谢。

1 个答案:

答案 0 :(得分:0)

使用filename.cpp:(.text+0x389): undefined reference to boost::filesystem::detail::status(boost::filesystem::path const&, boost::system::error_code*)' filename.cpp:(.text+0x39c): undefined reference to `boost::filesystem::detail::file_size(boost::filesystem::path const&, boost::system::error_code*)' obj/x86_64/release/filename.cpp.o: In function `readFromFileNotForm(boost::filesystem::path&, char*, unsigned int)': filename.cpp:(.text+0x1291): undefined reference to `boost::filesystem::detail::status(boost::filesystem::path const&, boost::system::error_code*)' obj/x86_64/release/filename.cpp.o: In function `readFromFileForm(boost::filesystem::path&, std::vector<unsigned int, std::allocator<unsigned int> >&)': filename.cpp:(.text+0x167a): undefined reference to `boost::filesystem::detail::status(boost::filesystem::path const&, boost::system::error_code*)' 代替#include "boost/filesystem/v3/operations.hpp" 解决此问题。