【问题标题】:Invalid literal for int() in Django modelDjango 模型中的 int() 文字无效
【发布时间】:2012-12-15 23:19:04
【问题描述】:

我收到一个错误的 int() 以 10 为底的无效文字:sharat 并且不知道如何修复它。有什么建议吗?

friend = 'sharat'
user_playlists = Everything.objects.filter(profile = friend).values('playlist').distinct()

class Everything(models.Model):
    profile = models.ForeignKey(User)
    playlist = models.CharField('Playlist', max_length = 2000, null=True, blank=True)
    platform = models.CharField('Platform', max_length = 2000, null=True, blank=True)
    video = models.CharField('VideoID', max_length = 2000, null=True, blank=True)
    video_title = models.CharField('Title of Video', max_length = 2000, null=True, blank=True)
    def __unicode__(self):
        return u'%s %s %s %s %s' % (self.profile, self.playlist, self.platform, self.video, self.video_title)

【问题讨论】:

    标签: python django exception model


    【解决方案1】:

    您首先需要按该名称获取用户,然后按该用户过滤,而不是按用户名过滤。或者(并且可能更好),您可以让 Django 以更有效的方式自行完成,可能涉及连接:

    user_playlists = Everything.objects.filter(profile__username=friend).values('playlist').distinct()
    

    【讨论】:

    • 没错。双下划线查询也是可能的!
    • 是的,答案是对的。评论也。喜欢:user_playlists = Everything.objects.filter(profile__username = friend).values('playlist').distinct()
    猜你喜欢
    • 1970-01-01
    • 2023-03-17
    • 2012-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多