4
我有一個模型管理一個簡單的模型:Django的模型保存,但返回無
class CompanyReviewManager(models.Manager):
def get_votes_for_company(self, company):
try:
return CompanyReview.objects.filter(user = user).count()
except ObjectDoesNotExist:
return None
def get_rating_for_field(self, installer, field):
try:
return CompanyReview.objects.filter(user = user).aggregate(Avg(field))
except ObjectDoesNotExist:
return None
class CompanyReview(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL)
satisfaction = models.PositiveSmallIntegerField(blank = True, null = True,)
comments = models.TextField(blank = True, null = True,)
objects = CompanyReviewManager()
def save(self, *args, **kwargs):
obj = super(InstallerReview, self).save(*args, **kwargs)
return obj
當我現在嘗試保存Django的殼對象,該對象將被保存,但沒有將被退回。爲什麼?
In [1]: company_obj = InstallerReview()
In [2]: company_obj.user = CompanyUser.objects.all()[2]
In [3]: obj = company_obj.save()
In [4]: obj
Out[4]:
In [5]: company_obj
Out[5]: <CompanyReview: AdminCompany>
爲什麼第三步失敗沒有一個錯誤?
@Urda我可能會錯過這裏的東西,但我不明白這是如何特別是1.5相關。爲什麼在標題中加入「Django」?它已被標記爲Django問題。 – Anto 2014-07-18 01:16:27