-17

这是带有回溯的更新代码

这是模型:

class Base(models.Model):
    created_by = models.ForeignKey(User, related_name="(app_label)s_%(class)s_creator")
    edited_by = models.ForeignKey(User, related_name="(app_label)s_%(class)s_editor")
    created_at = models.DateTimeField(auto_now_add=True)
    modified_at = models.DateTimeField(auto_now=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 in () ---- > 1 测试 = 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>'
4

1 回答 1

1

user创建部门时必须提供 a 。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,但 class 属性User.pk不是您可能认为的当前用户的 pk。

于 2013-05-13T08:35:24.220 回答