我们如何在Django中对连接表应用条件..?

时间:2014-11-13 12:34:22

标签: django python-2.7

我是Django的新手,在查询数据库方面遇到了问题。我有这样的模型:

class Messages(models.Model):
    class Meta:
        app_label = 'api'
        db_table = 'messages'
    from_user = models.ForeignKey('Users',related_name='from_user')    
    from_name = models.CharField(max_length=255,null=True, blank=True,default='')
    to_user   = models.ForeignKey('SpUsers',related_name='to_user')
    subject = models.TextField()
    message = models.TextField()
    replyId = models.IntegerField(default=0)
    topLevel_id = models.IntegerField(default=0)
    readFlag    = models.SmallIntegerField(default=0)
    deleteFlag    = models.SmallIntegerField(default=0)
    created_date = models.DateTimeField(auto_now_add=True)
    deleteFlag_c    = models.SmallIntegerField(default=0)
    deleteFlag_p    = models.SmallIntegerField(default=0)
    readFlag_c    = models.SmallIntegerField(default=0)
    readFlag_p    = models.SmallIntegerField(default=0)

和序列化器如:

class MessagesSerializer(serializers.ModelSerializer):
    from_user_id = serializers.PrimaryKeyRelatedField(source='from_user')
    from_avtar_url = serializers.RelatedField(source='from_user.avtar_url')
    from_user_details = serializers.RelatedField(source='from_user')
    from_user_status = serializers.RelatedField(source='from_user.status_id')

    to_user_id = serializers.PrimaryKeyRelatedField(source='to_user')
    to_avtar_url = serializers.RelatedField(source='to_user.avtar_url')
    to_user_details = serializers.RelatedField(source='to_user')
    to_user_status = serializers.RelatedField(source='to_user.status_id')

    to_user_services = serializers.RelatedField(source='to_user.service',many=True)
    class Meta:
        model = Messages
        fields = ('id','from_user_id','from_name','to_user_id','subject','message','replyId','topLevel_id','readFlag','readFlag_c','readFlag_p','deleteFlag','deleteFlag_c','deleteFlag_p','created_date','from_avtar_url','from_user_details','to_avtar_url','to_user_details','to_user_services','from_user_status','to_user_status')

现在我想对SpUsers.status_id中的queryset = queryset.filter(Q(from_user_id=from_user_id,deleteFlag=deleteFlag) | Q(to_user_id=to_user_id,deleteFlag=deleteFlag), to_user_status != 3)应用过滤器,但它无法正常工作并抛出以下错误:

NameError at /api/messages/

global name 'to_user_status' is not defined

Request Method:     GET
Request URL:    http://localhost:8000/api/messages/?from_user_id=13&to_user_id=13&deleteFlag=0&suspended=1
Django Version:     1.7
Exception Type:     NameError
Exception Value:    

global name 'to_user_status' is not defined

Exception Location:     /var/www/html/ovessence/ovessence-api/api/all_views.py in get_queryset, line 857
Python Executable:  /usr/bin/python
Python Version:     2.7.6
Python Path:    

['/var/www/html/ovessence/ovessence-api',
 '/usr/local/lib/python2.7/dist-packages/simplejson-3.6.5-py2.7-linux-x86_64.egg',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages/PILcompat',
 '/usr/lib/python2.7/dist-packages/gtk-2.0',
 '/usr/lib/python2.7/dist-packages/ubuntu-sso-client',
 '/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode',
 '/var/www/websites/ovessence/public_html/healthservices/healthservices']

Server time:    Thu, 13 Nov 2014 12:47:37 +0000

请帮助.. !!

1 个答案:

答案 0 :(得分:1)

!= filter()中没有运算符。你需要这样做:

queryset.filter(Q(from_user_id=from_user_id,deleteFlag=deleteFlag) | Q(to_user_id=to_user_id,deleteFlag=deleteFlag)).exclude(to_user_status=3)