无法查询"用户":必须是"模型"实例

时间:2017-01-09 18:51:35

标签: python django django-filter

我尝试将私人用户的用户过滤为用户聊天,我使用模型(buyerUser的用户名>和 seller的用户名与另一个模型(Profile)。

问题在于我不明白如何解决这个问题及其发生的原因:

  

ValueError:无法查询" user_39":必须是"个人资料"实例

在我看来:

uc = userComment.objects.all().first()
users = userComment.objects.filter(Q(buyer=uc.buyer) | Q(seller=uc.seller)) #error occurs with this line

pdb.set_trace()

(Pdb) uc.seller
<Profile: user_39>
(Pdb) uc.buyer
<User: user_4>

以下是我的两个模特。

class Profile(models.Model):
    name = models.CharField(max_length=120)
    user = models.OneToOneField(User, null=True, blank=True)

class userComment(models.Model):
    buyer = models.ForeignKey(User, related_name="buyer", null=True)
    seller = models.ForeignKey(Profile, related_name="seller", null=True)
    sent_at = models.DateTimeField(auto_now_add=True)
    comment = models.TextField(max_length=255, null=True)

在我的模特上,我不想将userComment.seller更改为ForeignKey(User, ...)

如何解决此问题?

所要求的完整代码:

views.py

def addComment(request, gig_id):
    gig = Gig.objects.get(id=gig_id)
    uc = userComment.objects.all().first()
    users = userComment.objects.filter(Q(Q(buyer=uc.buyer) & Q(seller=uc.seller)) | Q(Q(buyer=uc.seller) & Q(seller=uc.buyer))).order_by('sent_at')

    if request.method == 'POST':
        form = userCommentForm(request.POST, request.FILES)
        if form.is_valid():
            form.save(buyer=request.user)
            return HttpResponseRedirect('../' + str(gig.id))
    ...

forms.py

class userCommentForm(forms.Form):
    seller = CommaSeparatedUserField(label=_(u"seller"))
    comment = forms.CharField(label=_(u"comment"), widget=forms.Textarea())

    def save(self, buyer):
        sellers = self.cleaned_data['seller']
        comment = self.cleaned_data['comment']
        message_list = []

        for seller in sellers:
            sl = Profile.objects.get(user=seller) #Connection made here.
            msg = userComment(buyer=buyer, seller=sl, comment=comment,)
            ...
        ...

PDB调试

(Pdb) uc = userComment.objects.all().first()
(Pdb) print('Seller class: %s' % uc.seller.__class__.__name__)
Seller class: Profile
(Pdb) users = userComment.objects.filter(buyer_id=uc.buyer_id)
(Pdb) print(users.count())
3
(Pdb) users = userComment.objects.filter(seller_id=uc.seller_id)
(Pdb) print(users.count())
4
(Pdb) users = userComment.objects.filter(Q(seller_id=uc.seller_id) | Q(buyer_id=uc.buyer_id))
(Pdb) print(users.count())
4
(Pdb) users = userComment.objects.filter(buyer=uc.buyer)
(Pdb) print(users.count())
3
(Pdb) users = userComment.objects.filter(seller=uc.seller)

2 个答案:

答案 0 :(得分:3)

在您的模型userComment中,sellerForeignKeyProfile,但听起来您希望它位于User

class userComment(models.Model):
    buyer = models.ForeignKey(User, related_name="buyer", null=True)
    seller = models.ForeignKey(User, related_name="seller", null=True)

修改

很抱歉,我的答案太快了,您的错误基本上说:您在uc.buyer字段和buyer字段中使用条件seller查询,但seller定义为Profile的外键,因此您无法使用用户对象在Profile字段上进行查询。也许你想要这个呢?

users = userComment.objects.filter(Q(buyer=uc.buyer) | Q(seller=uc.seller))

<强>重新编辑:

应该使用User作为seller的外键userComment,因为这是一种关系的样子,你可以获得所有信息需要使用关系。使用User作为外键,您可以执行所有操作:

# get a user's profile
profile = user.profile
# query with profile's information on `userComment`
userComment.objects.filter(buyer__profile__name='Tom')

how to query across relationships上查看django doc。

答案 1 :(得分:1)

更新:哦,这是非常不同的代码,看起来你的实际查询是这样的:

userComment.objects.filter(Q(
    Q(buyer=uc.buyer) & Q(seller=uc.buyer)
) | Q(
    Q(buyer=uc.seller) & Q(seller=uc.buyer)
)).order_by('sent_at')

我会尝试:

userComment.objects.filter(Q(
    Q(buyer_id=uc.buyer_id) & Q(seller__user_id=uc.buyer_id)
) | Q(
    Q(buyer_id=uc.seller.user_id) & Q(seller__user_id=uc.buyer_id)
)).order_by('sent_at')

这很奇怪,因为你的代码乍一看似乎很好。我建议尝试以下代码来调试问题:

uc = userComment.objects.all().first()
print 'Seller class: %s' % uc.seller.__class__.__name__
users = userComment.objects.filter(buyer_id=uc.buyer_id)
print users.count()
users = userComment.objects.filter(seller_id=uc.seller_id)
print users.count()
users = userComment.objects.filter(Q(seller_id=uc.seller_id) | Q(buyer_id=uc.buyer_id))
print users.count()
users = userComment.objects.filter(buyer=uc.buyer)
print users.count()
users = userComment.objects.filter(seller=uc.seller)
print users.count()