问题描述:

I am trying to learn Django so I am pretty much a noob.

I have been trying to do the following simple task for two hours now. I know I have the wrong syntax, but searching Google & SO has not helped me, so I need some advice.

I am trying to update a db field from a Django template, but I need help in understanding how to update the language_code_disabled in my LanguageVersions views.py code below.

Here is my models.py file code:

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)

Here is my views.py code:

@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')

Excuse the simple question, but I am unable to get this correct.

网友答案:

You need to update field in the instance (not in the class) and call save() method of this instance.

language_version.language_code_disabled = False
language_version.save()
相关阅读:
Top