首页 \ 问答 \ Django自定义保存不工作(Django custom save not working)

Django自定义保存不工作(Django custom save not working)

所以我有以下型号:

class Info(models.Model):
    user_id = models.BigIntegerField(primary_key=True)
    #...
    def save(self,*args,**kwargs):
        print 1
        is_created = False
        if not self.pk:
            print 2
            is_created = True
        super(Info, self).save(*args, **kwargs)
        print 3
        if is_created:
            print 4
            signals.info_created.send(sender=self.__class__,info=self)

现在我有那些打印语句来跟踪发生了什么。当我运行Info.create(...)

info = Info(...)
info.save()

我得到以下输出:

1
3

这表明if not self.pk语句被评估为false。 现在我假设这是因为我有一个自定义主键需要在创建该对象时给出。 所以这是我的问题: 我的假设是正确的吗? 无论是否,我如何才能使这个自定义保存合作?


So I have the following model:

class Info(models.Model):
    user_id = models.BigIntegerField(primary_key=True)
    #...
    def save(self,*args,**kwargs):
        print 1
        is_created = False
        if not self.pk:
            print 2
            is_created = True
        super(Info, self).save(*args, **kwargs)
        print 3
        if is_created:
            print 4
            signals.info_created.send(sender=self.__class__,info=self)

now I have those print statements to keep track of what is going on.when I run either Info.create(...) or

info = Info(...)
info.save()

I get the following output:

1
3

which suggests that the if not self.pk statement evaluated to false. Now I assume this is because I have a custom primary key that needs to be given on the creation of that object. So here is my question: is my assumption correct? Regardless if it is or is not, how can I get this custom save to cooperate?


原文:
更新时间:2022-05-18 21:05

最满意答案

抛开模态(我在这里看不到模态代码),你可以像这样在你的+= " myBtn"行中添加一个空格,以成功地给按钮一个'myBtn'的ID。 如果没有,只需使用简单的“=”并为其提供所需的所有ID,以空格分隔。


Modal aside (I see no modal code here), you could probably add a space to your += " myBtn" line like so, to successfully give the button an ID of 'myBtn'. If not, just use a plain "=" and give it all the IDs it needs, separated by a space.

相关问答

更多
  • 您收到“Vue未定义”错误,因为您没有在HeightCard.js文件中导入Vue 。 但是,您无法正确导出Vue组件,因此即使将Vue导入该文件,您的HeightCard组件也将在导入它的文件中undefined 。 最简单的解决方案是将HeightCard组件更改为单个文件组件,就像父组件一样。 将文件HeightCard.vue ,然后将其结构如下: