RubyNoob RubyNoob - 19 days ago 4
Ajax Question

Django TypeError from Ajax action

I'm using DjangoTables to display records. I want to use Ajax on a Delete link to delete the relevant Site record (and the Deal records linked to it) without having to refresh the table page.

Clicking on 'Delete' deletes the relevant records and vanishes that row of the table as I wanted, but then I get this in my console:

TypeError: int() argument must be a string, a bytes-like object or a number, not 'builtin_function_or_method'
[19/Nov/2016 03:51:56] "GET /sites/tested/sites/delete/?recordid=1 HTTP/1.1" 500 18116


That doesn't look good. Why is it there, and how do I make it go away?

static .js file:

$(document).ready(function() {
$(".row-delete").click(function(event) {
event.preventDefault();
var recordid;
recordid = $(this).attr("data-recordid");
$.get('sites/delete/', {recordid: recordid}, function(data){
});
$(this).parent().parent().fadeOut("fast");
});
});


View:

class SiteDelete(View):
model=Site
def get (self, request):
site_id = None
if request.method == 'GET':
site_id = request.GET['recordid']
DealsToDelete = Deal.objects.filter(Site=id)
DealsToDelete.delete()
DeleteSite = Site.objects.get(id=site_id)
DeleteSite.delete()


Table

class TestedTable(tables.Table):
Approve = tables.TemplateColumn('<a href="{% url \"sites:approve\" id=record.id %}"</a>Approve')
Delete = tables.TemplateColumn('<a href="{% url \"sites:delete\" %}\" data-recordid = \"{{ record.id }}\" class=\"row-delete\">Delete</a>')
Name = tables.TemplateColumn('<a href="{% url \"deals:bysite\" record.id %}"</a>{{ record.Name }}')


urls.py

from django.conf.urls import url
from sites.views import SiteCreate, SiteUpdate, SiteDelete, IndexView, TestedView, DetailView, ApproveSite
from django.core.urlresolvers import reverse_lazy
app_name = 'sites'
urlpatterns = [
url(r'^$', IndexView.as_view(), name='site-list'),
url(r'add/$', SiteCreate.as_view(), name='site-add'),
url(r'(?P<pk>[0-9]+)/update/$', SiteUpdate.as_view(), name='site-update'),
url(r'delete/$', SiteDelete.as_view(), name='delete'),
url(r'tested/$', TestedView.as_view(), name='site-tested'),
url(r'(?P<pk>[0-9]+)/$', DetailView.as_view(), name='detail'),
url(r'(?P<id>[0-9]+)/approve/$', ApproveSite.as_view(), name='approve')


Detailed console output:

Internal Server Error: /sites/tested/sites/delete/
Traceback (most recent call last):
File "/home/danny/Documents/virtualenvs/AskArbyEnv/lib/python3.5/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/home/danny/Documents/virtualenvs/AskArbyEnv/lib/python3.5/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/danny/Documents/virtualenvs/AskArbyEnv/lib/python3.5/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/home/danny/Documents/virtualenvs/AskArbyEnv/lib/python3.5/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "/home/danny/Documents/LiClipse Workspace/AskArby/sites/views.py", line 34, in get
DealsToDelete = Deal.objects.filter(Site=id)
File "/home/danny/Documents/virtualenvs/AskArbyEnv/lib/python3.5/site-packages/django/db/models/manager.py", line 122, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/danny/Documents/virtualenvs/AskArbyEnv/lib/python3.5/site-packages/django/db/models/query.py", line 790, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/home/danny/Documents/virtualenvs/AskArbyEnv/lib/python3.5/site-packages/django/db/models/query.py", line 808, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/home/danny/Documents/virtualenvs/AskArbyEnv/lib/python3.5/site-packages/django/db/models/sql/query.py", line 1243, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "/home/danny/Documents/virtualenvs/AskArbyEnv/lib/python3.5/site-packages/django/db/models/sql/query.py", line 1269, in _add_q
allow_joins=allow_joins, split_subq=split_subq,
File "/home/danny/Documents/virtualenvs/AskArbyEnv/lib/python3.5/site-packages/django/db/models/sql/query.py", line 1199, in build_filter
condition = lookup_class(lhs, value)
File "/home/danny/Documents/virtualenvs/AskArbyEnv/lib/python3.5/site-packages/django/db/models/lookups.py", line 19, in __init__
self.rhs = self.get_prep_lookup()
File "/home/danny/Documents/virtualenvs/AskArbyEnv/lib/python3.5/site-packages/django/db/models/fields/related_lookups.py", line 100, in get_prep_lookup
self.lookup_name, self.rhs)
File "/home/danny/Documents/virtualenvs/AskArbyEnv/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 744, in get_prep_lookup
return self.get_prep_value(value)
File "/home/danny/Documents/virtualenvs/AskArbyEnv/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 976, in get_prep_value
return int(value)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'builtin_function_or_method'
[19/Nov/2016 03:51:56] "GET /sites/tested/sites/delete/?recordid=1 HTTP/1.1" 500 18116

Answer
DealsToDelete = Deal.objects.filter(Site=id)

In the above line, the coe is using id which is not defined by your code; so it uses the builtin function id.

>>> id
<built-in function id>

You should replace the id with site_id you defined.

DealsToDelete = Deal.objects.filter(Site=site_id)