当 type(self) 工作正常时,为什么 Loan 未定义?
管理员使用以下代码来保存 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Django 开发人员提供了一种覆盖模型的模式save() 方法。使用该模式,您的 save() 方法的实现应该是:
让我提供一种不同的方法:使用 信号!
不要尝试覆盖 save() 方法,而是使用 预保存信号 在插入/更新记录之前初始化 guid 字段。将以下代码添加到您的
model.py
文件中:现在,任何时候保存 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:
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: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.
还有一些事情正在将
Loan
重新绑定到None
。再往下看,或者也许在另一个模块中。另外,您不需要type(self)
因为这对于任何孙子或更进一步的子代都会失败。Something else is rebinding
Loan
toNone
. Look further down, or maybe in another module. Also, you don't wanttype(self)
since that will fail for any grandchildren or further.