Django ORM验证数据库中的一行
Django ORM verification of a row in the database
在我的网站上,买家搜索产品,找到产品后,他可以通过按联系按钮与卖家联系。如果两者之间存在关于该产品的对话,他应该被重定向到现有的对话,否则,我们创建一个新的对话。
因此,对话由两个用户和一个列表定义。
当我尝试实现逻辑时,我无法验证对话存在的两个条件,如果列表存在或用户存在 Django returns 则对话存在。这是我的代码:
def create_conversation(request, user_pk1, user_pk2, results_pk):
user1 = get_object_or_404(get_user_model(), pk=user_pk1)
user2 = get_object_or_404(get_user_model(), pk=user_pk2)
results= get_object_or_404(Listing, pk=results_pk)
existing_conversation = Conversation.objects.filter(users__in=[user1, user2]).filter(listing=results).values_list('id', flat=True)
if existing_conversation.exists():
return HttpResponseRedirect(reverse('conversation:conversation_update', kwargs={'pk':existing_conversation[0]}))
else:
conv=Conversation()
conv.save()
conv.listing = results
conv.save()
conv.users.add(*[user1,user2])
return HttpResponseRedirect(reverse('conversation:conversation_update', kwargs={'pk': conv.pk}))
这是对话的模型:
class Conversation(models.Model):
"""
Model to contain different messages between one or more users.
:users: Users participating in this conversation.
:archived_by: List of participants, who archived this conversation.
:notified: List of participants, who have received an email notification.
:unread_by: List of participants, who haven't read this conversation.]\['
listing: the listing the conversation is about.
read_by_all: Date all participants have marked this conversation as read.
"""
users = models.ManyToManyField(
settings.AUTH_USER_MODEL,
verbose_name=_('Users'),
related_name='conversations',
)
# review the listing before going online, because it is necessary to have a conversation listing
listing = models.ForeignKey (
Listing,
verbose_name=_('Listing'),
related_name='listing',
default= 1,
)
及刊登型号:
class 列表(models.Model):
seller = models.ForeignKey(Profile, related_name='seller_listing', verbose_name='sellr', limit_choices_to={'is_seller': True})
location_Country = models.CharField(max_length=45, verbose_name=_('from_Country'))
location_State = models.CharField(max_length=45, verbose_name=_('from_State'), null=True, blank=True)
location_City = models.CharField(max_length=45, verbose_name=_('from_City'))
我还尝试了一种将其分为两个条件的方法:a = conversation.objects.filter(users) 和 b= conversation.objects.filter(listing),然后使用 if a and b then the conversation exists but遇到了同样的问题。
and existing_conversation = Conversation.objects.filter(Q(users__in=[user1, user2]) & Q(listing=results)).values_list('id', flat=True) 但遇到了同样的问题。预先感谢您的帮助。
您可以使用 django 的 intersection()
方法,自 Django 1.11 以来添加的运算符 return 两个或多个 QuerySet 的共享元素或按位操作 AND
与符号 `& 一起使用以获得相同的行为。
所以在你的情况下,检查两个用户之间是否有 &
或 intersection()
的交集
existing_conversation = (<b>user1.conversations.all() & user2.conversations.all()</b>).filter(listing=results)
# or with django intersection
existing_conversation = (user1.conversations.all()<b>.intersection</b>(user2.conversations.all())).filter(listing=results)
if existing_conversation.exists():
return HttpResponseRedirect(reverse('conversation:conversation_update',
kwargs={'pk':<b>existing_conversation.first().pk</b>}))
else:
# rest of the code
BONUS, I see a typo in your code, you didn't send the pk as argument:
kwargs={'pk':<s>existing_conversation[0]</s>}
获取第一个实例 first()
并获取 pk
kwargs={'pk':<b>existing_conversation.first().pk</b>}
或
kwargs={'pk':<b>existing_conversation[0].pk</b>}
在我的网站上,买家搜索产品,找到产品后,他可以通过按联系按钮与卖家联系。如果两者之间存在关于该产品的对话,他应该被重定向到现有的对话,否则,我们创建一个新的对话。 因此,对话由两个用户和一个列表定义。 当我尝试实现逻辑时,我无法验证对话存在的两个条件,如果列表存在或用户存在 Django returns 则对话存在。这是我的代码:
def create_conversation(request, user_pk1, user_pk2, results_pk):
user1 = get_object_or_404(get_user_model(), pk=user_pk1)
user2 = get_object_or_404(get_user_model(), pk=user_pk2)
results= get_object_or_404(Listing, pk=results_pk)
existing_conversation = Conversation.objects.filter(users__in=[user1, user2]).filter(listing=results).values_list('id', flat=True)
if existing_conversation.exists():
return HttpResponseRedirect(reverse('conversation:conversation_update', kwargs={'pk':existing_conversation[0]}))
else:
conv=Conversation()
conv.save()
conv.listing = results
conv.save()
conv.users.add(*[user1,user2])
return HttpResponseRedirect(reverse('conversation:conversation_update', kwargs={'pk': conv.pk}))
这是对话的模型:
class Conversation(models.Model):
"""
Model to contain different messages between one or more users.
:users: Users participating in this conversation.
:archived_by: List of participants, who archived this conversation.
:notified: List of participants, who have received an email notification.
:unread_by: List of participants, who haven't read this conversation.]\['
listing: the listing the conversation is about.
read_by_all: Date all participants have marked this conversation as read.
"""
users = models.ManyToManyField(
settings.AUTH_USER_MODEL,
verbose_name=_('Users'),
related_name='conversations',
)
# review the listing before going online, because it is necessary to have a conversation listing
listing = models.ForeignKey (
Listing,
verbose_name=_('Listing'),
related_name='listing',
default= 1,
)
及刊登型号:
class 列表(models.Model):
seller = models.ForeignKey(Profile, related_name='seller_listing', verbose_name='sellr', limit_choices_to={'is_seller': True})
location_Country = models.CharField(max_length=45, verbose_name=_('from_Country'))
location_State = models.CharField(max_length=45, verbose_name=_('from_State'), null=True, blank=True)
location_City = models.CharField(max_length=45, verbose_name=_('from_City'))
我还尝试了一种将其分为两个条件的方法:a = conversation.objects.filter(users) 和 b= conversation.objects.filter(listing),然后使用 if a and b then the conversation exists but遇到了同样的问题。
and existing_conversation = Conversation.objects.filter(Q(users__in=[user1, user2]) & Q(listing=results)).values_list('id', flat=True) 但遇到了同样的问题。预先感谢您的帮助。
您可以使用 django 的 intersection()
方法,自 Django 1.11 以来添加的运算符 return 两个或多个 QuerySet 的共享元素或按位操作 AND
与符号 `& 一起使用以获得相同的行为。
所以在你的情况下,检查两个用户之间是否有 &
或 intersection()
existing_conversation = (<b>user1.conversations.all() & user2.conversations.all()</b>).filter(listing=results)
# or with django intersection
existing_conversation = (user1.conversations.all()<b>.intersection</b>(user2.conversations.all())).filter(listing=results)
if existing_conversation.exists():
return HttpResponseRedirect(reverse('conversation:conversation_update',
kwargs={'pk':<b>existing_conversation.first().pk</b>}))
else:
# rest of the code
BONUS, I see a typo in your code, you didn't send the pk as argument:
kwargs={'pk':<s>existing_conversation[0]</s>}
获取第一个实例 first()
并获取 pk
kwargs={'pk':<b>existing_conversation.first().pk</b>}
或
kwargs={'pk':<b>existing_conversation[0].pk</b>}