Class Views Confusion

Hello,
I am new to Django and have not been able to find an answer for a specific question I have. I have found answers that hit near what I need but nothing absolute so I hope I can explain my issue completely enough.

My app is data driven with a postgres db. When a user first sees my “customers” page I would like all existing db entries to be displayed. This part I am using ListView in my class based view. I would also like for these fields to be update-able, removable, as well as the ability to create new entries.

What is the proper way to implement this?

Are you talking about editing them directly in the ListView? Or are you looking for something similar to the admin where you select an entry from the list, which takes you to an edit page?

I was hoping to be able to do everything from one page but that is not a requirement, so selecting one and going to an edit/create/delete page is an options I am willing to implement as well.

Both are possible.

For the “same page” solution, what you’re looking for is a formset.

For the multi-page solution, your link on the ListView element would take you to an EditView. You would also want to add links on that list for a CreateView and DeleteView.

@KenWhitesell Thank you for your assistance!!

I have implemented the formset and am getting what I am looking for display wise (showing all customers +1 empty field for a new entry). I am receiving data into the request.post conditional check , however it is not passing formset.is_valid() check. lol

Hang in there. Personally, I’ve found formsets to be the most difficult part of Django I’ve had to wrap my head around. I know it took me a while to get comfortable using them.

I’ve gotten to the point where I can display all existing entries +1 empty. Now if I leave the +1 blank I can update the existing entries just fine, but if I try to add and entry it fails .is_valid(). Checking the errors this is because I have a hidden input with the customer_id. I am using bulk_update() to update the existing entries which requires the hidden input value. If I use bulk_create() I do not need the hidden input but it will create the new entry and create duplicates of the existing entries. Is there a way to avoid this?

Yes, use neither. Rely upon the formset’s save method to do the right thing. See the docs on Saving objects in the formset. It will also handle the situation where you’ve marked an instance for deletion.