我在Django
应用程序中使用MySQL
数据库,但当我尝试保存模型时,它返回以下错误:
(1241, 'Operand should contain 1 column(s)')
这是我的代码:
型号
class Proposal(models.Model):
"""
Model for Proposals
"""
status_choices = (
("pending", _("Pending")),
("accepted", _("Accepted")),
("declined", _("Declined")),
)
related_request = models.ForeignKey(Request, on_delete=models.CASCADE, verbose_name=_('Request'),
help_text=_('Request'))
user = models.ForeignKey(CustomUser, on_delete=models.CASCADE, verbose_name=_('User'), help_text=_('User'))
price = models.IntegerField(verbose_name=_('Price'), help_text=_('Price'))
notes = models.TextField(verbose_name=_('Notes'), help_text=_('Notes'), null=True, blank=True)
estimated_time = models.CharField(max_length=100, verbose_name=_('Estimated time'), help_text=_('Estimated time'))
date_created = models.DateTimeField(verbose_name=_('Date created'), help_text=_('Date created'), auto_now_add=True)
date_modified = models.DateTimeField(verbose_name=_('Date modified'), help_text=_('Date modified'), auto_now=True)
status = models.CharField(max_length=100, verbose_name=_('Proposal status'),
help_text=_('Proposal status'),
choices=status_choices, default="pending")
checked_by_admin = models.BooleanField(verbose_name=_("Checked by admins"), default=False, help_text=_(
"Check this only if you are an admin and toke actions with this request"))
file = models.FileField(upload_to="proposals_files",
help_text='File attached with the proposal',
verbose_name='Attached file',
null=True,
)
client_notes = models.TextField(verbose_name=_('Client Notes'), help_text=_('Client Notes'), null=True, blank=True)
def __str__(self):
return self.related_request.type + ' ' + self.related_request.finishing_type
class Meta:
verbose_name = _('Proposal')
verbose_name_plural = _('Proposals')
它返回两个站的错误:
1-当我尝试从Admin interface. 2- when I try to save or edit using
Django rest框架视图保存、编辑或创建时。
DRF视图
@api_view(["POST"])
@permission_classes([IsAuthenticated])
def react_to_proposal(request):
"""
Api Function that applies the client reaction to proposals
"""
if request.method == "POST":
proposal_id = request.data['proposal_id']
is_accept = request.data['is_accept']
notes = request.data['notes']
print(proposal_id)
print(is_accept)
print(notes)
proposal_object = Proposal.objects.get(id=proposal_id)
proposal_object.status = "accepted" if is_accept else "declined"
proposal_object.client_notes = notes
proposal_object.save()
此外,我有这个信号作为模型的post_save
:
@receiver(post_save, sender=Proposal)
def proposal_post_save(sender, instance, created, **kwargs):
"""
Signal for post saving proposal
"""
if instance.checked_by_admin and instance.status == "pending":
send_notification_to_one(instance.related_request.user.id, "A supplier has sent a proposal on your request")
elif instance.checked_by_admin and instance.status == "accepted":
send_notification_to_one(instance.user.id, f"Your proposal {instance.id} has been accepted, "
f"Rolla technical team will contact team shortly "
f"to close the deal")
elif instance.checked_by_admin and instance.status == "rejected":
send_notification_to_one(instance.user.id, f"Your proposal {instance.id} has been rejected")
open_proposal_count = Proposal.objects.filter(status="pending", related_request=instance.related_request)
instance.related_request.number_of_proposals = open_proposal_count
instance.related_request.save()
发现我忘记在信号中的查询末尾添加.count()
@receiver(post_save, sender=Proposal)
def proposal_post_save(sender, instance, created, **kwargs):
"""
Signal for post saving proposal
"""
if instance.checked_by_admin and instance.status == "pending":
send_notification_to_one(instance.related_request.user.id, "A supplier has sent a proposal on your request")
elif instance.checked_by_admin and instance.status == "accepted":
send_notification_to_one(instance.user.id, f"Your proposal {instance.id} has been accepted, "
f"Rolla technical team will contact team shortly "
f"to close the deal")
elif instance.checked_by_admin and instance.status == "rejected":
send_notification_to_one(instance.user.id, f"Your proposal {instance.id} has been rejected")
open_proposal_count = Proposal.objects.filter(status="pending", related_request=instance.related_request).count() //here was the problem
instance.related_request.number_of_proposals = open_proposal_count
instance.related_request.save()