Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set django model instance field like dictionary

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.

like image 697
Muoi Nguyen Avatar asked Apr 30 '26 09:04

Muoi Nguyen


1 Answers

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]
like image 190
Mike Olszak Avatar answered May 01 '26 21:05

Mike Olszak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!