__init__ method called every time on same APIView object

261 views
Skip to first unread message

Diana María Bedoya Ramírez

unread,
Oct 30, 2019, 7:10:08 PM10/30/19
to Django users
Hello,

I have an APIView object with an __init__method. In that __init__ I have a loop that extends a class list variable. Every time I make a POST to the url that calls the APIView, the items in the loop items are added to the old ones in the list, duplicating the item number every time. 

Why is the __init__ method called on the same object with every request? How can assure a new object with every requesdt I make?


Juan Pablo Romero Bernal

unread,
Oct 31, 2019, 1:21:58 AM10/31/19
to django...@googlegroups.com
Hi, 
Show some example of code ....

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/452af061-bf26-4a2a-9437-eea482f951e1%40googlegroups.com.


--
Juan 

Diana María Bedoya Ramírez

unread,
Oct 31, 2019, 9:43:50 AM10/31/19
to Django users
urls.py:

urlpatterns = [
...
path('incoming-calls/', incoming_calls.IncomingCallsReport.as_view(), name='incoming-calls'),
...
]

incoming_calls.py

class IncomingCallsReport(APIView):
    select_fields = ["uniqueid", "enterdate"]

    def __init__(self):
        show_contact_details = settings.SHOW_CONTACT_DETAILS
        if show_contact_details:
            for i in range(11):
                self.select_fields.append("custom{}".format(i + 1))

    def post(self, request):               
        ...
        return Response(status=200)    


With the code above, every time I make a POST to incoming-calls/, the field select_fields gets 10 new custom fields, and that's not the idea

Andréas Kühne

unread,
Nov 1, 2019, 10:55:52 AM11/1/19
to django...@googlegroups.com
Hi,

This is a python problem - You are not using an instance variable for the select_fields variable, but a class variable. If you want to use it as an instance variable you need to change the code to this:
class IncomingCallsReport(APIView):
   

    def __init__(self):
        self.select_fields = ["uniqueid", "enterdate"]
        show_contact_details = settings.SHOW_CONTACT_DETAILS
        if show_contact_details:
            for i in range(11):
                self.select_fields.append("custom{}".format(i + 1))

This way you will get an instance variable. The code that you provided creates the select_fields variable on the class the first time the class is instanciated and not every time it is created.

Best regards,


Andréas


--
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.

Diana María Bedoya Ramírez

unread,
Nov 1, 2019, 1:42:57 PM11/1/19
to Django users
Thank you very much Andréas. The program is working fine now. I didn't know the difference between those two variable types, but now I understand.


On Wednesday, October 30, 2019 at 6:10:08 PM UTC-5, Diana María Bedoya Ramírez wrote:
Reply all
Reply to author
Forward
0 new messages