[Answered ]-When I create an object in Django admin, `form_valid()` method is not called

1👍

A ModelAdmin has no .form_valid(…) method [Django-doc], this is for class-based views (CBVs) that inherit from a FormMixin [Django-doc]. The ModelAdmin will call the .save_model(…) method [Django-doc], so you should implement the logic in that method:

class MyModelAdmin(admin.ModelAdmin):
    # …
    
    def save_model(self, request, obj, form, change):
        obj.author = request.user
        super().save_model(request, obj, form)
        bid = round(float(form.cleaned_data['starting_bid']), 2)
        Bid.objects.create(
            bid_value = bid,
            bidder = request.user,
            item=obj
        )

Leave a comment