Q:

'float' object has no attribute 'set' in Django

am new to django & python. Im trying to build a site where users can leave bids on items. The amount they leave bidded is registered as floats. When trying to save the bid, i get the error 'float' object has no attribute 'set'. Why is that? Im not sure how to fix it.
models.py, Bid has a manytomany field.
class User(AbstractUser):
    pass

class Listing(models.Model):
    title = models.CharField(max_length=100)
    image = models.ImageField(blank=True, upload_to='media', default="noimage.jpg")
    content = models.TextField()
    date_posted = models.DateTimeField(default=timezone.now)
    categories = models.CharField(blank=False, max_length=25, choices=category, default="Others")
    seller = models.ForeignKey(User, on_delete=models.CASCADE) ##
    min_bid = models.FloatField(blank=False, validators=[MinValueValidator(0)])

    image_thumbnail = ImageSpecField(source='image', processors=[ResizeToFill(300, 150)], format='JPEG', options={'quality':100})
    image_thumbnail1 = ImageSpecField(source='image', processors=[ResizeToFill(1200, 500)], format='JPEG', options={'quality':100})

    def get_absolute_url(self):
        return reverse('listing-detail', kwargs={'pk': self.pk})

class Bid(models.Model):
    bid = models.FloatField(validators=[MinValueValidator(0)])
    bidder = models.ManyToManyField(User, default=None)
    listed_item = models.ForeignKey(Listing, on_delete=models.CASCADE, default=None)

    def get_absolute_url(self):
        return reverse('listing-detail', kwargs={'pk': self.pk})

views.py:
method_decorator(login_required, name='post')
class ListingDetailView(DetailView):
    model = Listing

    def post(self, request, pk):
        user = request.user
        bidder = User.objects.filter(username=user)

        #gets the amount from form
        new_bid = float(request.POST["user_bid"])
        item = Listing.objects.get(pk=pk)

        try:
            bid = Bid.objects.get(listed_item=item)
            highest_bid = bid.bid
        except Bid.DoesNotExist:
            bid = None
            highest_bid = item.min_bid

        if new_bid < highest_bid:
            messages.add_message(request, messages.WARNING, "Bid is too low.")

        else:
#error highlights this try: line
            try:
                bid = Bid.objects.get(listed_item=item, bidder=user)
                bid.bid = new_bid
                bid.save()

            except Bid.DoesNotExist:
#since no bids exist, i believe here is the issue at .set

                b = Bid.objects.create(bid=new_bid, listed_item=item)
                b.bid.set(bidder)

            item.min_bid = new_bid
            item.save()
            messages.add_message(request, messages.SUCCESS,
                                 "Bid placed successfully.")

        return redirect('listing-detail', self.kwargs['pk'])

error traceback:
Traceback (most recent call last):
  File "C:\Users\AG\Desktop\cs50\commerce\auctions\views.py", line 137, in post
    bid = Bid.objects.get(listed_item=item, bidder=user)
  File "C:\Python\Python38\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Python\Python38\lib\site-packages\django\db\models\query.py", line 415, in get
    raise self.model.DoesNotExist(

During handling of the above exception (Bid matching query does not exist.), another exception occurred:
  File "C:\Python\Python38\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Python\Python38\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Python\Python38\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Python\Python38\lib\site-packages\django\views\generic\base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Python\Python38\lib\site-packages\django\views\generic\base.py", line 97, in dispatch
    return handler(request, *args, **kwargs)
  File "C:\Python\Python38\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper
    return bound_method(*args, **kwargs)
  File "C:\Python\Python38\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "C:\Users\AG\Desktop\cs50\commerce\auctions\views.py", line 143, in post
    b.bid.set(bidder)

Exception Type: AttributeError at /listing/11/
Exception Value: 'float' object has no attribute 'set'

A:

Looks like you need to set User object to bidder
Try:
b.bidder.set(bidder)

