I am a beginner and trying to implement bid system in Django. I want it to work on both Django admin page and and template, therefore I created modelform and modeladmin in Admins.py.
models.py:
from django.contrib.auth.models import AbstractUser
from django.db import models
class User(AbstractUser):
pass
class category(models.Model):
category = models.CharField(max_length=50, default='SOME STRING')
def __str__(self):
return f"{self.category}"
class bid(models.Model):
listing = models.ForeignKey('listing', on_delete=models.CASCADE)
user = models.ForeignKey(User, null=True, on_delete=models.CASCADE)
bid = models.DecimalField(max_digits=6, null=True, decimal_places=2)
def __str__(self):
return f"{self.user}, {self.listing} {self.bid}"
class listing(models.Model):
user = models.ForeignKey(User, null=True, on_delete=models.CASCADE)
Title = models.CharField(max_length=50)
Description = models.CharField(max_length=300)
Price = models.DecimalField(max_digits=6, null=True, decimal_places=2)
category = models.ForeignKey(category, on_delete=models.CASCADE, related_name="categories")
def __str__(self):
return f"{self.Title}"
admin.py
from django.contrib import admin
from .models import User, listing, category, bid
from django.core.exceptions import ValidationError
from django import forms
admin.site.register(User)
admin.site.register(listing)
admin.site.register(category)
class bidForm(forms.ModelForm):
class Meta:
model=bid
fields = ['user', 'listing', 'bid']
def clean(self):
start_price = self.cleaned_data.get('listing.Price')
userbid = self.cleaned_data.get('bid')
if userbid <= start_price:
raise ValidationError('Please place a bid higher than starting price')
return self.cleaned_data
class bidAdmin(admin.ModelAdmin):
form = bidForm
list_display = ('user', 'listing', 'bid')
admin.site.register(bid, bidAdmin)
It returns the following error: '<=' not supported between instances of 'decimal.Decimal' and 'NoneType'. Also I want to compare instances of previous and current bid on a listing to place a new bid, also modify any of previously placed bids even if it's lower than highest bid. but I have no idea how to code that. Please, help me...