当 type(self) 工作正常时,为什么 Loan 未定义?

发布于 2024-09-30 04:30:38 字数 2198 浏览 4 评论 0原文

管理员使用以下代码来保存 Loan 对象

import uuid

from django.db import models
from django.contrib.auth.models import User
from apps.partners.models import Agent

# Create your models here.
class Loan(models.Model):
    """ This is our local info about the loan from the LOS """
    guid = models.CharField(max_length=64, blank=True)
    number = models.CharField(max_length=64, blank=True)
    name = models.CharField(max_length=64)
    address = models.CharField(max_length=128)
    address2 = models.CharField(max_length=128, null=True, blank=True)
    city = models.CharField(max_length=32)
    state = models.CharField(max_length=2)
    zipcode = models.CharField(max_length=9)
    borrowers = models.ManyToManyField(User, related_name='loan_borrowers')
    officers = models.ManyToManyField(Agent, related_name='loan_officers')

    def __unicode__(self):
        return "%s %s, %s  %s" % (self.address, self.city, self.state, self.zipcode)

    def save(self, force_insert=False, force_update=False, using=None):
        """ Adds a GUID if one is not present """
        if self.guid == None:
            self.guid = uuid.uuid4().hex
        super(Loan, self).save(force_insert, force_update, using)

当我到达超级行时,我得到:

TypeError: super() argument 1 must be type, not None

save 调用是从 options.py 第 597 行进行的,此时 obj 已知是一个 Loan 对象。

如果我用 super() 行替换

    super(type(self), self).save(force_insert, force_update, using)

一切都很好。这是怎么回事?

文件的其余部分是:

class Need(models.Model):
    from apps.public_portal.models import DocumentType
    loan = models.ForeignKey(Loan, null=False, blank=False)
    borrower = models.ForeignKey(User, null=False, blank=False)
    doctype = models.ForeignKey(DocumentType, null=False, blank=False)
    satisfied = models.DateTimeField(null=True, blank=True)
    first_request = models.DateTimeField(auto_now_add=True)
    last_request = models.DateTimeField(null=True, blank=True)

    def __unicode__(self):
        return "%s from %s for %s" % (self.doctype.name, self.borrower.get_full_name(), self.loan)

所以我不明白有什么东西是如何将 Loan 绑定到 None 的

The following code is being used by the admin to save a Loan object

import uuid

from django.db import models
from django.contrib.auth.models import User
from apps.partners.models import Agent

# Create your models here.
class Loan(models.Model):
    """ This is our local info about the loan from the LOS """
    guid = models.CharField(max_length=64, blank=True)
    number = models.CharField(max_length=64, blank=True)
    name = models.CharField(max_length=64)
    address = models.CharField(max_length=128)
    address2 = models.CharField(max_length=128, null=True, blank=True)
    city = models.CharField(max_length=32)
    state = models.CharField(max_length=2)
    zipcode = models.CharField(max_length=9)
    borrowers = models.ManyToManyField(User, related_name='loan_borrowers')
    officers = models.ManyToManyField(Agent, related_name='loan_officers')

    def __unicode__(self):
        return "%s %s, %s  %s" % (self.address, self.city, self.state, self.zipcode)

    def save(self, force_insert=False, force_update=False, using=None):
        """ Adds a GUID if one is not present """
        if self.guid == None:
            self.guid = uuid.uuid4().hex
        super(Loan, self).save(force_insert, force_update, using)

When I get to the super line, I get:

TypeError: super() argument 1 must be type, not None

The save call is made from options.py line 597 and at that point obj is known to be a Loan object.

if I replace the super() line with

    super(type(self), self).save(force_insert, force_update, using)

all is well. What is going on here?

The rest of the file is:

class Need(models.Model):
    from apps.public_portal.models import DocumentType
    loan = models.ForeignKey(Loan, null=False, blank=False)
    borrower = models.ForeignKey(User, null=False, blank=False)
    doctype = models.ForeignKey(DocumentType, null=False, blank=False)
    satisfied = models.DateTimeField(null=True, blank=True)
    first_request = models.DateTimeField(auto_now_add=True)
    last_request = models.DateTimeField(null=True, blank=True)

    def __unicode__(self):
        return "%s from %s for %s" % (self.doctype.name, self.borrower.get_full_name(), self.loan)

So I don't see how anything is binding Loan to None

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不…忘初心 2024-10-07 04:30:38

Django 开发人员提供了一种覆盖模型的模式save() 方法。使用该模式,您的 save() 方法的实现应该是:

def save(self, *args, **kwargs):
    if self.guid == None:
        self.guid = uuid.uuid4().hex
    super(Blog, self).save(*args, **kwargs) # Call the "real" save() method.

让我提供一种不同的方法:使用 信号

不要尝试覆盖 save() 方法,而是使用 预保存信号 在插入/更新记录之前初始化 guid 字段。将以下代码添加到您的 model.py 文件中:

def add_loan_guid( sender, instance, **kwargs ):
    """Ensure that a Loan always has a guid"""
    if instance.guid == None:
        instance.guid = uuid.uuid4().hex

pre_save.connect( add_loan_guid, sender=Loan )

现在,任何时候保存 Loan 对象时,但在将其持久保存到数据库之前,都会调用 add_loan_guid(),并且如果 Loan 实例没有设置 guid,将创建一个新的。

The Django developers offer a pattern for overriding the model save() method. Using that pattern, the implementation for your save() method should be:

def save(self, *args, **kwargs):
    if self.guid == None:
        self.guid = uuid.uuid4().hex
    super(Blog, self).save(*args, **kwargs) # Call the "real" save() method.

Let me offer a different approach: use signals!

Instead of trying to override the save() method, use a pre-save signal to initialize the guid field prior to the record being inserted/updated. Add the following code to your model.py file:

def add_loan_guid( sender, instance, **kwargs ):
    """Ensure that a Loan always has a guid"""
    if instance.guid == None:
        instance.guid = uuid.uuid4().hex

pre_save.connect( add_loan_guid, sender=Loan )

Now, any time that a Loan object is saved, but before it is persisted to the database, add_loan_guid() will be called, and if the Loan instance has no guid set, a new one will be created.

冰魂雪魄 2024-10-07 04:30:38

还有一些事情正在将 Loan 重新绑定到 None。再往下看,或者也许在另一个模块中。另外,您不需要 type(self) 因为这对于任何孙子或更进一步的子代都会失败。

Something else is rebinding Loan to None. Look further down, or maybe in another module. Also, you don't want type(self) since that will fail for any grandchildren or further.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文