ValueError:无法分配" 4749890231992320L":" Student.user"必须是"用户"实例。 (Django +定制型号)

时间:2014-05-19 14:59:34

标签: python django django-models

我在将Model.objects.get与自定义模型一起使用时遇到问题,该模型包含标准Django用户作为其中一个字段。以下是基本属性:

class Student(models.Model):
    user = models.ForeignKey(User, unique=True)
    school = models.ForeignKey(School)
    isHead = models.BooleanField()

我制作了一个新的SchoolUserStudent,并毫无问题地保存了所有内容。所以我将以下内容输入manage.py shell,其中newUser对象的变量:

Student.objects.get(user = new)

然后,我收到错误:

ValueError: Cannot assign "5312840185413632L": "Student.user" must be a "User" instance.

我做了一些研究,我理解上面的意思是说我传递了一些一个User对象作为参数。但是,我已经定义了

new = User(username="new user")

所以我无法弄清楚我做错了什么。

非常感谢任何帮助。如果它有帮助,我在Eclipse中工作,这对我来说是新的,虽然Django / python不是。

编辑:这是我的自定义类的完整代码:

class School(models.Model):
    def level(self):
        return get_max_triangle_num_less_than(self.rep / SCHOOL_REP_LEVEL_MODIFIER)

    name = models.CharField(max_length=64)
    # Rep could also be a DecimalField (needs max_digits and decimal_places specified)
    rep = models.IntegerField(default = 0)
    num_students = models.IntegerField(default = 0)

    def __init__(self, name, *args, **kwargs):
        models.Model.__init__(self, *args, **kwargs)
        self.name = name

    def __str__(self, *args, **kwargs):
        return self.name + ", " + str(self.num_students) + " students"

class Student(models.Model):

    def level(self):
        return get_max_triangle_num_less_than(self.rep / STUDENT_REP_LEVEL_MODIFIER)

    user = models.ForeignKey(User, unique=True)
    school = models.ForeignKey(School)
    isHead = models.BooleanField()

    referral_code = models.CharField(max_length=128, unique=True)
    referred_by = models.ForeignKey(User, blank=True, null=True)

    rep = models.IntegerField(default = 0)

    def __init__(self, user, school, referred_by = None, *args, **kwargs):
        models.Model.__init__(self, *args, **kwargs)
        self.user = user
        self.school = school
        if referred_by:
            self.referred_by = referred_by

        # This automatically creates a custom string of letters/numbers -- I thought it was 
        #  the easiest way to make a custom "referral code" which can be used to link a 
        #  registration to the referrer
        self.referral_code = str(uuid.uuid4())

        self.school.num_students += 1

    def __str__(self):
        something = str(self.user.username)
        if self.isHead:
            something += ", Head at "
        else:
            something += ", at "
        something += self.school.name + ", " + str(self.rep) + " rep points"
        return something

以下是我在shell + traceback中输入的内容:

>>> from spudmart.campusrep.models import User, School, Student
>>> usr = User(username="user")
>>> usr.save()
>>> sch = School(name = "school")
>>> sch.save()
>>> stu = Student(user = usr, school = sch)
>>> stu.save()
>>> Student.objects.get(user = usr)

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/Lucy/Code/Git/spudder/spudmart/src/django/db/models/manager.py", line 132, in get
    return self.get_query_set().get(*args, **kwargs)
  File "/Users/Lucy/Code/Git/spudder/spudmart/src/django/db/models/query.py", line 346, in get
    num = len(clone)
  File "/Users/Lucy/Code/Git/spudder/spudmart/src/django/db/models/query.py", line 82, in __len__
    self._result_cache = list(self.iterator())
  File "/Users/Lucy/Code/Git/spudder/spudmart/src/django/db/models/query.py", line 288, in iterator
    obj = model(*row[index_start:aggregate_start], **{'__entity_exists': True})
  File "/Users/Lucy/Code/Git/spudder/spudmart/src/spudmart/campusrep/models.py", line 51, in __init__
    self.user = user
  File "/Users/Lucy/Code/Git/spudder/spudmart/src/django/db/models/fields/related.py", line 331, in __set__
    self.field.name, self.field.rel.to._meta.object_name))
ValueError: Cannot assign "4661929301770240L": "Student.user" must be a "User" instance.

2 个答案:

答案 0 :(得分:0)

在仔细观察追溯 - 我本来应该首先做的 - 我尝试评论出代码以覆盖__init__方法。瞧,它有效!故事的道德:在询问互联网之前,仔细观察追溯。感谢大家的帮助!

答案 1 :(得分:0)

你在模型__init__中做了一些奇怪的事情,你需要将所有这些逻辑移到save()方法,必须是这样的:

class Student(models.Model):

def level(self):
    return get_max_triangle_num_less_than(self.rep / STUDENT_REP_LEVEL_MODIFIER)

user = models.ForeignKey(User, unique=True)
school = models.ForeignKey(School)
isHead = models.BooleanField()

referral_code = models.CharField(max_length=128, unique=True)
referred_by = models.ForeignKey(User, blank=True, null=True)

rep = models.IntegerField(default = 0)

def __str__(self):
    something = str(self.user.username)
    if self.isHead:
        something += ", Head at "
    else:
        something += ", at "
    something += self.school.name + ", " + str(self.rep) + " rep points"
    return something

def save(self, *args, **kwargs):
    if self.pk is None:  # if this is new object (not update)
        self.school.num_students += 1
        self.referral_code = str(uuid.uuid4())
    instance = super(Student, self).save(*args, **kwargs)
    return instance