为什么我得到这个Django TypeError?

时间:2017-11-20 20:18:29

标签: python django

我收到以下错误:

  

TypeError在/ admin / KSUvity / activity / add / coercing to Unicode:need   字符串或缓冲区,找到用户

models.py:

class Activity(models.Model):
    type = models.CharField(max_length=50, default="")
    title = models.CharField(max_length=200, default="")
    description = models.CharField(max_length=500)
    owner = models.ForeignKey(User, related_name="owner")
    college = models.CharField(max_length=200)
    location = models.CharField(max_length=200)
    room = models.CharField(max_length=200)
    startDate = models.DateTimeField(null=True, blank=True)
    endDate = models.DateTimeField(null=True, blank=True)
    attendee = models.ManyToManyField(Attendee, related_name="attendees",null=True, blank=True)
    volunteer = models.ManyToManyField(Volunteer, related_name="volunteers",null=True, blank=True)
    created_time = models.DateTimeField(editable=False, auto_now= True)
    modified_time = models.DateTimeField(null=True, blank=True)

    def __str__(self):
        return '%s (%s)' % (self.title, self.description)

    def __unicode__(self):
        return unicode(self.user)

    def save(self, *args, **kwargs):
        ''' On save, update timestamps '''
        if not self.id:
            self.created = timezone.now()
        self.modified = timezone.now()
        return super(Activity, self).save(*args, **kwargs)

class Attendee(models.Model):
    student = models.ForeignKey(User, related_name="attendee")

    def __str__(self):
        return self.student

在forms.py中:

class ActivityForm(forms.ModelForm):

    class Meta:
        model = Activity
        fields = ('type', 'title', 'description', 'owner', 'college', 'location', 'room', 'startDate', 'endDate', 'attendee', 'volunteer', )

从管理页面,我曾经获得一个活动表单并添加活动。 但我突然开始得到这个错误。 问题可能是什么?

1 个答案:

答案 0 :(得分:1)

尝试改变

def __str__(self):
    return self.student

def __str__(self):
    return '%s' % self.student