0

I'm in a little gambit with this question here "conversion from method to Decimal is not supported django" error where adding the () gives me an error 'DeferredAttribute' object is not callable and removing them gives me another conversion from DeferredAttribute to Decimal is not supported (My current error with the code below)

My goal is two take my 3 values and figure out what the profit or loss is:

Profit_long = (price 1 - price 2) * size

*I'm not entriely sure if the math syntax is right yet because the above error message won't let me get that far. From what I've read as long as those variables are integers the math syntax should be right. Not sure if it will work with decimals. That's step two. First I need to solve the above issue.

views.py

class TradeDetailView(DetailView):
template_name = 'tj/cp/trade/detail.html'

queryset = Trade.objects.all()

def get_object(self):
    id_ = self.kwargs.get("id")
    return get_object_or_404(Trade, id=id_)

def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    entry_price = Decimal(Trade.entry_price)
    exit_price = Decimal(Trade.entry_price)
    size = Decimal(Trade.size)

    context['profit_long'] = (entry_price - exit_price) * size

    return context

models.py

class Trade(models.Model):
    ...
    size = models.DecimalField(null=True, blank=True, max_digits=50, decimal_places=2)
    entry_price = models.DecimalField(null=True, blank=True, max_digits=50, decimal_places=2)
    exit_price = models.DecimalField(null=True, blank=True, max_digits=50, decimal_places=2)
    ...
Alex Winkler
  • 469
  • 4
  • 25

1 Answers1

1

I'm modified your code for correct work:

class TradeDetailView(DetailView):
    template_name = 'tj/cp/trade/detail.html'
    pk_url_kwarg = 'id'
    model = Trade

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        entry_price = Decimal(self.object.entry_price)
        exit_price = Decimal(self.object.exit_price)
        size = Decimal(self.object.size)
        context['profit_long'] = (entry_price - exit_price) * size
        return context

But it is better to do differently. Add a property to model Trade:

class Trade(models.Model):
    # ...
    size = models.DecimalField(null=True, blank=True, max_digits=50, decimal_places=2)
    entry_price = models.DecimalField(null=True, blank=True, max_digits=50, decimal_places=2)
    exit_price = models.DecimalField(null=True, blank=True, max_digits=50, decimal_places=2)
    # ...

    @property
    def profit_long(self):
        return (self.entry_price - self.exit_price) * self.size

Then a view will become more concise.:

class TradeDetailView(DetailView):
    template_name = 'tj/cp/trade/detail.html'
    pk_url_kwarg = 'id'
    model = Trade
    context_object_name = 'trade'

And use property in django template:

<p>Profit long: {{ trade.profit_long }}</p>
crazyzubr
  • 1,072
  • 7
  • 14
  • 1
    Check the fields for emptiness leave at your disposal. – crazyzubr Oct 27 '18 at 16:25
  • Both was work, amazing. I really like the @property method. Didn't know about it and didn't even find much about it on Djangos docs. Thanks a lot and sorry for the late response! One question, what is the "pk_url_kwarg = 'id'"for? I left it out and everything still works. – Alex Winkler Oct 28 '18 at 10:04
  • https://docs.djangoproject.com/en/2.1/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin.pk_url_kwarg – crazyzubr Oct 28 '18 at 10:19
  • @property decorator need to look in the python documentation https://docs.python.org/3/library/functions.html#property – crazyzubr Oct 28 '18 at 10:21