django tables 2 - delete column and delete_item for inherited tables

218 views
Skip to first unread message

Nader Elsisi

unread,
Jun 5, 2020, 4:49:17 PM6/5/20
to django...@googlegroups.com

I have just posted 

https://stackoverflow.com/questions/62218886/django-tables-2-delete-column-and-delete-item-for-inherited-tables 

and didn't get any feedback yet. So I appreciate anyone who can help me

about passing data. 


I want to have one abstract function for all my tablelists (one for each model) and (one delete_item) function in view.

I don't know how to make the delete (column in this table) and pass the model to the delete_item function in the view

Tables.py


    ############ Abstract Table
    class abs_Table(tables.Table):

    SN = tables.Column(empty_values=(), orderable=False)
    delete = tables.LinkColumn('delete_item', args=[A('pk'), ?????Model???],  attrs={
         'a': {'class': 'btn btn-small btn-dark'}
    # })


    def __init__(self, *args, **kwargs):
        super(abs_Table, self).__init__(*args, **kwargs)
        self.counter = itertools.count(1)


    def render_SN(self, record):

        pg = getattr(self, 'paginator', None)
        if pg:
            v = next(self.counter)
            return v + self.paginator.per_page * (self.page.number-1)
        else:
            return next(self.counter)


    class Meta:
        model = None
        fields = [ 'SN', 'id',  'delete', ]

        attrs = {"class": "table-striped table-bordered", 'width': '100%'}
        empty_text = "There are no Records matching the search criteria..."

Then for model Table

Tables.py


    class ModelTable(abs_Table):


    class Meta(abs_Table.Meta):
        model = modelname
        fields = abs_Table.Meta.fields+[selected_model_fields]

Views.py



      def delete_item(request, pk, delmodel):
        obj = get_object_or_404(delmodel, id=pk)


        if request.method == "POST":

                    obj.delete()

            return redirect("../")
        else:
            pass


        context = {
            'object': obj
        }

        return render(request, '/delete_confirmation.html', context)

wongX Ndeso

unread,
Jun 7, 2020, 6:38:09 AM6/7/20
to django...@googlegroups.com
Try django generic edit view (CBV).
You can use DeleteView Class to perform what you need to do.. 

--
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/CA%2BkREvqnPgZ9z9j93PVd32F6dGcdtFe1dNuPkA--qx6YecJEOQ%40mail.gmail.com.

wongX Ndeso

unread,
Jun 7, 2020, 6:41:37 AM6/7/20
to django...@googlegroups.com
If you use django to perform some delete action, or others like create or update, please make sure you have csrf_token in your html form file, and then you should filter if the form is valid or not, then you can process the data that send from delete page.


On Sat, Jun 6, 2020, 3:48 AM Nader Elsisi <nader...@gmail.com> wrote:

Nader Elsisi

unread,
Jun 7, 2020, 7:48:37 AM6/7/20
to django...@googlegroups.com
Thanks I hope you get what I am asking for. I want to be able to delete from the list. Not one object. Also, l want one function for different models.
I am doing it now but I need to improve it. So l asked for help.

wongX Ndeso

unread,
Jun 7, 2020, 8:31:12 AM6/7/20
to django...@googlegroups.com
So what you mean is bulk delete?  Is that what you asking? 

Nader Elsisi

unread,
Jun 7, 2020, 3:52:10 PM6/7/20
to django...@googlegroups.com
Single records but from listview

Reply all
Reply to author
Forward
0 new messages