我创建了一个模型,该模型使用content_types框架和相关视图来添加和跟踪给定用户的点。当我使用django shell中的视图时,一切正常,但是当我尝试在我的项目中使用它时,我得到完整性错误。
我有以下模型(points.models)
class PointLedger(models.Model):
class Meta:
verbose_name_plural = 'Point Ledger Entries'
created_datetime = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey(User)
points = models.IntegerField()
note = models.CharField(max_length=255,blank=True,null=True)
length = models.IntegerField(blank=True,null=True)
action = models.CharField(max_length=255)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
我还有以下视图代码(points.views)
def add_points(content_object, user, points, action, note=''):
pl = PointLedger(content_object=content_object,
user=user, points=points, action=action, note=note)
pl.save()
当我在交互式django shell中时,我可以毫无问题地使用add_points
from points.views import add_points
comment = Comment.objects.all()[12]
user = User.objects.all()[2]
add_points(comment, user, 5, 'made')
#This works and updates PointLedger
但是当我尝试在项目中的视图中使用app_points
时,我会收到错误:
def comment_add_points(sender, **kwargs):
points = settings.MODEL_POINT_VALUES['comment']
comment = kwargs['comment']
user = User.objects.get(username=comment.user.username)
add_points(comment, user, points, 'made')
错误:
IntegrityError at /comments/post/
null value in column "object_id" violates not-null constraint
DETAIL: Failing row contains (19, 2014-10-23 18:06:30.767924+00, 1, 5, , null, 9, null, made).
有人能告诉我哪里出错了吗?此外,如果有人对此代码有任何提示,我将非常感谢。