Total Pageviews

Tuesday, 21 October 2014

LIST field in Django

If you need to use list as a field type in Django then you just need to some steps and you will be done with it. Firstly add this code snippet in models.py where you will be using list as a field type.
import ast
class ListField(models.TextField):
    __metaclass__ = models.SubfieldBase
    description = "Stores a python list"

    def __init__(self, *args, **kwargs):
       super(ListField, self).__init__(*args, **kwargs)

    def to_python(self, value):
       if not value:
           value = []

       if isinstance(value, list):
           return value

       return ast.literal_eval(value)

    def get_prep_value(self, value):
       if value is None:
           return value

       return unicode(value)

    def value_to_string(self, obj):
       value = self._get_val_from_obj(obj)
       return self.get_db_prep_value(value)
Then you can add the attribute in the class where list field is required,
                                       tracking_ques = ListField()
Here, tracking_ques is supposed to be attribute of type list. Even you can go to sqlite and use edit tables option to add attribute of type listfield. We basically used list field to keep track of people liking a particular question. We stored the user IDs of people liking particular question.
                          
              y_list = []
    pre = request.session['ra_no']

    q1 = answers.objects.get(aid=aid)
    f_list = q1.tracked_by
    for x in f_list:
       q = ast.literal_eval(x)
       y_list.append(q)
    if int(pre) not in y_list:

     q1.tracked_by.append(pre)
     q1.save()
    return HttpResponseRedirect('/thanks/')        
This code initially involves creation of empty list.
                                                      y_list = []
Then we have have stored the unique id ra_no in variable called pre using sessions[This is used to get the unique ID of the person who has liked the particular answer]
                                                       pre  =  request.session['ra_no']
Then we have taken the particular answer which person has liked and got the list of people who have liked it via this line,
      
                                               f_list = q1.tracked_by
Now comes the important part of it, The list f_list will be in the form, [u’123’,u’124’,u’125’] even though we have appended 123,124 and 125 only. So we need to remove those extra characters which is done using the next line, we have retrieved each list item applied ast.literal_eval and removed the extra characters and put all the items in new list called y_list.
                                                  for x in f_list:
                                                     q = ast.literal_eval(x)
                                                     y_list.append(q)
  
The list y_list is of the form, [123,124,125] The next part is implementation of simple logic, if the person has already liked the answer then we do not add his/her ID to the list but has liked the answer for the first time then we do add his/her ID to the list.
        
                                   if int(pre) not in y_list:
                                               q1.tracked_by.append(pre)
                                              q1.save()

No comments:

Post a Comment