Reputation: 33
In django, we can update model instance field like:
from .models import Credentials
current_cre=Credentials.object.get(user=request.user)
current_cre.fb_name='Name'
current_cre.fb_pass='****'
current_cre.save()
Is there any way that I can do it like dictionary item:
current_cre['fb_name']='Name'
current_cre['fb_pass']='Pass'
So I can iterate the process like:
for field_name in List_Fields:
current_cre[field_name]=request.POST[field_name]
thanks in advance.
Upvotes: 3
Views: 563
Reputation: 88569
You could use the inbuilt __dict__
for this, as
from .models import Credentials
current_cre = Credentials.object.get(user=request.user)
current_cre.__dict__['fb_name'] = "Name"
current_cre.__dict__['fb_pass'] = "****"
current_cre.save()
if user
is a unique
field, then you could try this also,
from .models import Credentials
my_dict = {"fb_name": "Name", "fb_pass": "****"}
current_cre = Credentials.object.filtert(user=request.user).update(**my_dict)
Upvotes: 1
Reputation: 403
Try this
List_Fields = [(f.verbose_name, f.name) for f in Credentials._meta.get_fields()]
for field_name, value in List_Fields:
current_cre[field_name]=request.POST[field_name]
Upvotes: 2