Updating multiple records at once

Hi, I have this model:

#models.py
class ItemRequest(models.Model):
    boat = models.ForeignKey(Boat, on_delete=models.CASCADE, null=True, blank=True)
    created_by = models.ForeignKey(Person, on_delete=models.SET_NULL, null=True, blank=True)
    created_date = models.DateTimeField(default=datetime.now, blank=True)
    item = models.CharField(max_length=200)
    quantity = models.PositiveIntegerField()

    CONDITION_STATUS = (
        ('C', 'Dispatched'),
        ('p', 'Pending'),
        ('m', 'Not in stock'),
    )

    CONDITION_RECEIVED = (
        ('n', 'Not Received'),
        ('r', 'Received'),
    )

    status = models.CharField(
        max_length=1,
        choices=CONDITION_STATUS,
        blank=True,
        default='p',
    )

    received = models.CharField(
        max_length=1,
        choices=CONDITION_RECEIVED,
        blank=True,
        default='n',
    )

With the help of a modelformset_factory, I can insert items. one by one.
and with a generic.UpdateView I could update the items ‘individually’ with a Template link like this:

#item_list.html
   ........
 <a href="{% url 'items-detail' i.id %}">update</a>
   ........

#urls.py
  path('items-detail/<int:pk>/', views.ItemListUpdate.as_view(), name='items-detail')

Following is my Update View:

#views.py
class ItemListUpdate(LoginRequiredMixin, generic.UpdateView):
    model = ItemRequest
    fields = ['status']
    context_object_name = 'item'
    template_name = 'item_update.html'

    def get_queryset(self):
        return super().get_queryset().select_related('boat__captain__name', 'created_by')

    def get_success_url(self):
        return reverse('items')

However I want to be able to update the statuses of multiple items depending upon a day so that, I do not need to update every single item individually. Hope I make myself clear.

That’s the purpose of model formsets - to display multiple instances of a form, and allow you to update them all at once.

However, when using formsets, I recommend you either

  • use a function-based view instead of a CBV

or, if you’re really determined to use a CBV

  • build your view on FormView, and not on any of the generic edit views.

Thanks for the pointer :+1:.