get data from model with field name in django 1

get data from model with field name in django

field_name = "is_staff"
user = User.objects.get(pk=0)
field_name_val = getattr(user, field_name)

Here is what the above code is Doing:
1. Get the User object with primary key 0.
2. Get the value of the field named “is_staff” from the User object.
3. Assign the value to the variable field_name_val.

Similar Posts