How to access Django data with the field name in text?

32 views
Skip to first unread message

rmschne

unread,
Nov 14, 2016, 6:38:05 AM11/14/16
to Django users
I have a extracted all the field names from a Django model into a list called "fields", e.g.

fields = [(f.name, f.verbose_name) for f in Meetingattendee._meta.get_fields()]

I ask the user to select a record, e.g.

att=Meetingattendee.objects.get(id=attid) 

where "attid" is the ID of the record (input by the user)

I now will ask user to input the name of the field of interest, e.g. "fname" which is one of the fields and holds "first name",  I then want to enable viewing/changing the selected field, e.g. in code to work with the first name field, it would be simply att.fname, but in this case the name of the field a string (input from user).  How to convert the input string name "fname" into something that in code would recognise that the user wants to work with att.fname?

(hoping this question is clear!)

--rms



Vijay Khemlani

unread,
Nov 14, 2016, 6:58:06 AM11/14/16
to django...@googlegroups.com
If I understood it correctly, you might want to do it like this

setattr(att, "fname", "value_to_be_set")

and to get the value

first_name = getattr(att, "fname")

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/df713caf-24f3-4309-b270-0b58e12c75cc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

rmschne

unread,
Nov 14, 2016, 7:14:35 AM11/14/16
to Django users
yep, that did it. It was getattr() and setattr() I was unaware of (or forgot!).  

I made my code a bit more generic (and I know I have not put in much error correction or exception detection, but will do so).  Thank you.  See code here:

fields = [(f.name, f.verbose_name) for f in Meetingattendee._meta.get_fields()]
    s=''
    for i in fields:
        s=s+i[0]+", "
    fieldtochange=raw_input("Enter field to change:\n"+s+": ")
    print "Current Value of Field to Change:",fieldtochange,"is:",getattr(att, fieldtochange)
    newvalue=raw_input("Enter New Value: ")
    if not newvalue:
        return None
    setattr(att, fieldtochange, newvalue)
    att.save()
    print "Saved:",att


On Monday, 14 November 2016 11:58:06 UTC, Vijay Khemlani wrote:
If I understood it correctly, you might want to do it like this

setattr(att, "fname", "value_to_be_set")

and to get the value

first_name = getattr(att, "fname")
On Mon, Nov 14, 2016 at 8:38 AM, rmschne <rms...@gmail.com> wrote:
I have a extracted all the field names from a Django model into a list called "fields", e.g.

fields = [(f.name, f.verbose_name) for f in Meetingattendee._meta.get_fields()]

I ask the user to select a record, e.g.

att=Meetingattendee.objects.get(id=attid) 

where "attid" is the ID of the record (input by the user)

I now will ask user to input the name of the field of interest, e.g. "fname" which is one of the fields and holds "first name",  I then want to enable viewing/changing the selected field, e.g. in code to work with the first name field, it would be simply att.fname, but in this case the name of the field a string (input from user).  How to convert the input string name "fname" into something that in code would recognise that the user wants to work with att.fname?

(hoping this question is clear!)

--rms



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages