我正在尝试建立一个拍卖网站,并且我有一个ForeignKey字段,如果可能的话,我可能想添加一些额外的参数。
现在,当我进入/ admin并进入“列表”并单击列表时,有一个用于Winners的下拉选择器。但是下拉选择器向我显示了所有已放置的出价,包括放置在其他列表上的出价。我如何获得下拉选择器,以便仅允许我从对特定列表进行了出价的用户中进行选择。我假设我需要在获奖者字段中添加某种条件?
models.py
class Listing(models.Model):
class NewManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(status='active')
options = (
('active', 'Active'),
('closed', 'Closed'),
)
title = models.CharField(max_length=64)
description = models.TextField(max_length=64)
start_price = models.DecimalField(max_digits=9, decimal_places=2, validators=[MinValueValidator(0.99)])
image = models.URLField(max_length=200, blank=True)
category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name="listings")
lister = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, default=None, null=True, blank=True, related_name="lister_user")
date_added = models.DateTimeField(default=timezone.now)
status = models.CharField(max_length=10, choices=options, default="active")
winner = models.ForeignKey('Bid', on_delete=models.CASCADE, null=True, related_name="bid_winner")
favourites = models.ManyToManyField(User, related_name="favourite", default=None, blank=True)
objects = models.Manager()
listingmanager = NewManager()
def __str__(self):
return f"{self.title} ({self.pk}, £{self.start_price}, {self.lister})"
class Bid(models.Model):
bidder = models.ForeignKey(User, on_delete=models.CASCADE, related_name="bidders")
bid_item = models.ForeignKey(Listing, on_delete=models.CASCADE, related_name="bid_items", default=None)
bid_input = models.DecimalField(max_digits=9, decimal_places=2, default=None)
time = models.DateTimeField(default=timezone.now)
def __str__(self):
return f"{self.bidder}, bid amount: {self.bid_input}"
我不确定是否需要 forms.py ,但会以防万一。一个单独的问题,但希望有人可以提供帮助。如果出价输入低于标价或最高出价,则下面的BidForm表单验证不会引发验证错误。谁能看到我在哪里出问题了?
class NewListingForm(forms.ModelForm):
class Meta:
model = Listing
fields = ["title", "description", "start_price", "image", "category"]
title = forms.CharField(widget=forms.TextInput(attrs={'autocomplete':'off'}))
description = forms.CharField(widget=forms.TextInput(attrs={'autocomplete':'off'}))
start_price = forms.DecimalField(label='Starting Bid Price (£)')
image = forms.URLField(widget=forms.URLInput(attrs={'autocomplete':'off'}))
category = forms.ModelChoiceField(queryset=Category.objects.all())
class BidForm(forms.ModelForm):
class Meta:
model = Bid
fields = ["bid_input"]
labels = {"bid_input": ""}
widgets = {
"bid_input": forms.NumberInput(attrs={'placeholder': 'Enter bid (£)'})
}
def clean_bid_input(self):
input_bid = self.cleaned_data['bid_input']
item_bid = self.cleaned_data['bid_item']
highest_bid = Bid.objects.filter(item_bid).aggregate(Max('bid_input'))
listing_price = Listing.objects.get('bid_item').start_price
if highest_bid is None:
if input_bid < listing_price:
raise ValidationError('Bid must be higher than listing start price')
else:
if input_bid < highest_bid:
raise ValidationError('Bid must be higher than current highest bid')
return input_bid