无法向Charfield添加数据

时间:2014-10-13 18:49:16

标签: python django

奇怪的错误。当我尝试将一些数据添加到我的字段时,它会显示如下错误:invalid literal for int() with base 10:

这是我的模特:

class Follower(models.Model):
    follower = models.CharField(max_length=140)
    def __unicode__(self):
        return self.follower

class Following(models.Model):
    following = models.CharField(max_length=140) 
    def __unicode__(self):
        return self.following

class UserProfile(models.Model):
    # This line is required. Links UserProfile to a User model instance.
    user = models.OneToOneField(User)
    # The additional attributes we wish to include.
    website = models.URLField()
    followers = models.ManyToManyField(Follower)
    following = models.ManyToManyField(Following)

查看:

if request.GET.get('follow'):
        author = UserProfile.objects.get(user__username__iexact=username)
        b = "AAA"
        author.followers.add(b)

怎么办?

1 个答案:

答案 0 :(得分:2)

followers不是CharField,而是ManyToManyField。您不能只为其添加文本:您需要创建一个Follower实例,或者获取一个现有实例,然后添加它。