I'm creating a crypto-currency application. In my app a user can have many transactions and a transaction can consist of many sales. I have a form for entering sales and I'm trying to put the max_value constraint equal to a property (coins_remaining) in the transaction table. Is it possible to do this? If so how would I go about it?

Sale Form Below

class SaleForm(forms.ModelForm):

    amount_sold = forms.IntegerField(min_value=0.1, label='Enter number of coins sold ',
                                     error_messages={'min_value': 'You must enter some coins '}
    total_price_sold = forms.DecimalField()
    date_sold = forms.DateField(
                                label='Enter Date of Sale ',
                                help_text='Please use calendar by clicking arrow on the right ',
                                    attrs={'type': 'date'}
    note = forms.CharField(label='Write a note below ',
                           widget=forms.TextInput(attrs={'placeholder': 'Keep a Note? '}))

    class Meta:
        model = Sale
        fields = ('date_sold', 'amount_sold', 'total_price_sold', 'note')

Transaction model below

class Transaction(models.Model):
    currency = models.CharField(max_length=20)
    amount = models.IntegerField()
    total_price = models.DecimalField(max_digits=8, decimal_places=2)
    date_purchased = models.DateTimeField()
    note = models.TextField(default="")
    owner = models.ForeignKey(User, on_delete=models.CASCADE)
    amount_per_coin = models.DecimalField(max_digits=8, decimal_places=2, editable=False)

    def save(self, *args, **kwargs):
        self.amount_per_coin = self.total_price / self.amount
        super(Transaction, self).save(*args, **kwargs)

    def __str__(self):
        return str(self.pk)+','+self.currency + ', '+str(self.amount)

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

    def coins_remaining(self):
        return self.amount - sum(self.sales.all().values_list('amount_sold', flat=True))

Sale model below

class Sale(models.Model):
    amount_sold = models.IntegerField()
    total_price_sold = models.DecimalField(max_digits=8, decimal_places=2)
    date_sold = models.DateTimeField(default=timezone.now)
    note = models.TextField(default="")
    transaction = models.ForeignKey(Transaction, on_delete=models.CASCADE, related_name="sales")
    amount_per_coin_sold = models.DecimalField(max_digits=8, decimal_places=2, editable=False)

    def __str__(self):
        return str(self.pk)+','+str(self.amount_sold) + ', '+self.note

    def save(self, *args, **kwargs):
        self.amount_per_coin_sold = self.total_price_sold / self.amount_sold
        super(Sale, self).save(*args, **kwargs)

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

Function Below

class SaleCreateView(LoginRequiredMixin, CreateView):
    form_class = SaleForm
    template_name = "webapp/sale_form.html"

    def dispatch(self, request, *args, **kwargs):

        self.transaction = get_object_or_404(Transaction, pk=kwargs['pk'])
        return super().dispatch(request, *args, **kwargs)

    def form_valid(self, form):

        form.instance.transaction = self.transaction
        return super().form_valid(form)

0 Answers