Django更新字段

时间:2015-01-03 02:25:15

标签: python django django-views

我正在努力学习Django,所以我几乎都是菜鸟。

我一直在尝试做两个小时的以下简单任务。我知道我的语法错误,但搜索Google&所以没有帮助我,所以我需要一些建议。

我正在尝试从Django模板更新数据库字段,但我需要帮助了解如何在我的 LanguageVersions views.py代码中更新 language_code_disabled

这是我的models.py文件代码:

class LanguageVersion(models.Model):
    """Language version selection for a user"""
    user = models.ForeignKey(User)
    language_code = models.CharField(max_length=32)
    language_code_disabled = models.BooleanField(default=False)

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

这是我的views.py代码:

@login_required
def language_versions_enable(request):
    if request.method == 'POST':
        language_version = LanguageVersion.objects.get(pk=language_version_id, user=request.user)
    # don't enable Language Version for currently selected language preference.
    if language_version.language_code != request.user.userprofile.language_preference:
        if language_version.language_code_disabled == True:
            # if the users Language Version is disabled, enable the disabled Language Version.
            LanguageVersion.language_code_disabled = False  # THIS IS THE LINE OF CODE I DO NOT UNDERSTAND HOW TO WRITE!
            messages.success(request,_('successfully enabled.'))
    return redirect('language_versions')

请原谅这个简单的问题,但我无法理解这一点。

1 个答案:

答案 0 :(得分:1)

您需要更新实例中的字段(不在类中)并调用此实例的save()方法。

language_version.language_code_disabled = False
language_version.save()