[Answered ]-TypeError at /listing: save() missing 1 required positional argument: 'self'

1👍

The modeling is quite odd. An AuctionListing refers to a Bids model, but it makes not much sense to do that. In case each AcutionListing has a Bids object and vice-versa, it is more sensical to just add the data to the AuctionListing itself.

Furthermore you do not store the individual bids. If later the auction is closed, how will you know who made the highest bid? Your Bids model also creates a list, and appends the bid field to that, but that is the only item it will contain. The logic in the class is only evaluated once: when the class is constructed, so from the moment the class has been interpreted, the bid and numBids do not make much sense anymore.

The Bids also contain a field minimum_bid, but this is information related to the object people are bidding on, not a bid itself.

A more sensical modeling is that you have an AuctionListing to present objects on which one can bid. Per AuctionListing there can be multiple Bids, one can then determine the lowest bid by annotating, or making queries. In that case the two models are thus defined as:

class AuctionListing(models.Model):
    title = models.CharField(max_length=30)
    description = models.CharField(max_length=137)
    category = models.CharField(max_length=10, choices=Category.CHOICES, default='Men')
    image = models.ImageField(upload_to='media')
    minimum_bid = models.DecimalField(max_digits=8, decimal_places=2)

    def __str__(self):
        return f'{self.category}: {self.title}'

    def get_largest_bit(self):
        return self.bids.order_by('-bid').first()

Then you can define a Bid model with a ForeignKey to the AuctionList: the item that people can bid on:

class Bid(models.Model):
    item = models.ForeignKey(
        AuctionListing,
        on_delete=models.CASCADE,
        related_name='bids'
    )
    bid = models.DecimalField(max_digits=8, decimal_places=2)

In your view, you can then make a bid on the AuctionList item with as primary key pk with:

from django.shortcuts import get_object_or_404

def make_bid(request, pk):
    item = get_object_or_404(AuctionList, pk=pk)
    if request.method == 'POST':
        bid_value = request.POST['bid']  # retrieve the bid value from somewhere
        if bid_value < item.minimum_bid:
            # do something, report an error
            # …
        else:
            Bid.objects.create(item_id=pk, bid=bid_value)
            return redirect('name-of-some-view')
    else:
        # …
    # …

Leave a comment