繁体   English   中英

将 Django 与 MySQL 一起使用时如何解决错误(1241,'操作数应包含 1 列')

[英]how to solve error (1241, 'Operand should contain 1 column(s)') when using Django with MySQL

我将MySQL数据库与我的Django应用程序一起使用,但是当我尝试保存 model 时,它返回此错误:

(1241, 'Operand should contain 1 column(s)')

这是我的代码:

Model

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 Admin interface. 2- when I try to save or edit using

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的 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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM