对象不可编写脚本意味着什么?

发布于 2024-10-10 08:58:40 字数 1087 浏览 3 评论 0原文

我不知道这里发生了什么...我只想检查模型字段的值,然后相应地更新它...任何帮助或见解表示赞赏!

模型:

class UserProfile(models.Model):
    user = models.ForeignKey(User, unique=True)
    beta = models.CharField(max_length=1, blank=True, null=True)

视图:

from internal.accounts.models import UserProfile
from django.contrib.auth.models import User
@login_required    
def beta_testers(request):
    user = User.objects.get(username=request.user.username)
    user_profile = user.get_profile()

    count = UserProfile.objects.filter(beta='1').count()

    if count < 50 and not user_profile['beta']:
        user_profile['beta'] = '1'
        user_profile.save()

错误:

TypeError at /utilities/beta-signup/
'UserProfile' object is unsubscriptable
Request Method: GET
Request URL:    http://localhost/utilities/beta-signup/?x=1&y=15
Django Version: 1.2.1
Exception Type: TypeError
Exception Value:    
'UserProfile' object is unsubscriptable
Exception Location: C:/django\internal\cms_helper\views.py in beta_testers, line 284

I don't know what's going on here... I just want to check the value of a model field and then update it accordingly... any help or insight is appreciated!

model:

class UserProfile(models.Model):
    user = models.ForeignKey(User, unique=True)
    beta = models.CharField(max_length=1, blank=True, null=True)

view:

from internal.accounts.models import UserProfile
from django.contrib.auth.models import User
@login_required    
def beta_testers(request):
    user = User.objects.get(username=request.user.username)
    user_profile = user.get_profile()

    count = UserProfile.objects.filter(beta='1').count()

    if count < 50 and not user_profile['beta']:
        user_profile['beta'] = '1'
        user_profile.save()

error:

TypeError at /utilities/beta-signup/
'UserProfile' object is unsubscriptable
Request Method: GET
Request URL:    http://localhost/utilities/beta-signup/?x=1&y=15
Django Version: 1.2.1
Exception Type: TypeError
Exception Value:    
'UserProfile' object is unsubscriptable
Exception Location: C:/django\internal\cms_helper\views.py in beta_testers, line 284

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

娇纵 2024-10-17 08:58:40

错误是“不可订阅”。您的 user_profile 对象不是字典。使用 user_profile.beta,而不是 user_profile['beta']

The error is "unSUBscriptable". Your user_profile object isn't a dictionary. Use user_profile.beta, not user_profile['beta'].

忘你却要生生世世 2024-10-17 08:58:40

或者,您可以使用带有 getattr 的字符串:

getattr(user_profile, 'beta', False)

False 是默认值;在您的情况下,这将用于检查该值是否已设置。我发现这非常有帮助,所以我想我会发布这个解决方案,即使这个问题是几年前提出的。 :)

Alternatively, you can use a string with getattr:

getattr(user_profile, 'beta', False)

False is the default value; which, in your case would work with checking if the value is set or not. I found this very helpful, so I thought I would post this solution, even though the question was asked years ago. :)

此刻的回忆 2024-10-17 08:58:40

方法一

model_name.object.filter(column_name='value').last

方法二

当您尝试在文本字段中插入模型对象时会发生此错误


Method One

model_name.object.filter(column_name='value').last

Method Two

This error happens when you tries to insert a model object in text filed

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文