以下是带有追溯的更新代码
以下是模型:
class Base(models.Model):
created_by = models.ForeignKey(User,default=User.pk, related_name="(app_label)s_%(class)s_creator")
created_by = models.ForeignKey(User,default=User.pk, related_name="(app_label)s_%(class)s_editor")
created_at = models.DateTimeField(auto_now_add=True)
modified_at = models.DateTimeField(auto_now_add=True)
class Meta:
abstract = True
class Department(Base):
dept_id = models.AutoField(primary_key=True)
name = models.CharField(max_length=60, unique=True)
description = models.TextField()
def __unicode__(self):
return str(self.name)
错误回溯: ValueError Traceback(最近一次调用最后一次) ()中的C:\ python27 \ lib \ site-packages \ django-1.5-py2.7.egg \ django \ core \ management \ commands \ shell.pyc ----> 1 test = Department.objects.create(name ='test',description ='test')
C:\python27\lib\site-packages\django-1.5-py2.7.egg\django\db\models\fields\__init__.pyc in get_prep_value(se
lf, value)
553 if value is None:
554 return None
--> 555 return int(value)
556
557 def contribute_to_class(self, cls, name):
ValueError: invalid literal for int() with base 10: '<property object at 0x000000000332AB38>'
答案 0 :(得分:0)
创建部门时,您必须提供user
。如果您在视图中,则可以request.user
访问当前用户
user = request.user
如果您正在shell中进行测试,请或从数据库中获取任何用户
user = User.objects.get(id=1) # get
然后创建department
:
department = Department(name='test',
description='test',
user=user,
)
最后,您的default=User.pk
不正确。对于用户实例,您可以访问user.pk
,但类属性User.pk
不是您可能认为的当前用户的pk。