Arbazz Hussain
Arbazz Hussain

Reputation: 1932

How to retrieve the field values from model objects

I have the following model which contains 2 List Field's:

class Cimex_Search(models.Model):
    search_engine = ArrayField(models.TextField(blank=True),blank=True,null=True,default=list)
    web_technology = ArrayField(models.TextField(blank=True),blank=True,null=True,default=list)

    def __str__(self):
        return "default"

Now I have the following function in views.py:

def cimex_search_searcher(request):
        default_table = Cimex_Search.objects.get(id=1)
        field_type = request.GET.get('fieldtype')
        print(default_table.field_type) ###! NEED HELP HERE

What is the best way to tackle this problem?

Upvotes: 1

Views: 65

Answers (1)

Sergio
Sergio

Reputation: 334

You can try using getattr passing the model and field, so for your use case that would be:

def cimex_search_searcher(request):
    default_table = Cimex_Search.objects.get(id=1)
    field_type = request.GET.get('fieldtype')
    print(getattr(default_table, field_type)) 

Upvotes: 1

Related Questions