Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
496 views
in Technique[技术] by (71.8m points)

django models - If and else Statement in Boolean Field

my question seems tricky to me, but i'm certain someone could derive a solution to it. i've a boolean field, but i want to add a functionality in which when the boolean is been clicked (True), i could implement an {% if %} and {% else %} which could be, add a particular amount to the original amount, if the boolean field is True. my code is below for proper understanding...

class OrderItem(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    ordered = models.BooleanField(default=False)
    item = models.ForeignKey(Item, on_delete=models.CASCADE)
    paper = models.BooleanField(default=False, blank=True)
     

    def get_final_price(self):
        if self.item.discount_price:
            return self.get_total_discount_price()
        return self.get_total_item_price()

    def coverframe(self):
        return get_final_price() + 3000
    

class Order(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    item = models.ManyToManyField(OrderItem)
    
    def __str__(self):
        return self.user.username

    def get_total_everything(self):
        total = 0
        for order_item in self.item.all():
            total += order_item.get_final_price() 
        return total

        def get_total_everything_cover_paper(self):
        total = 0
        for order_item in self.item.all():
            total += order_item.coverframe() 
        return total



Views
class OrderSummary(LoginRequiredMixin, View):
    def get(self, *args, **kwargs):
        try:
            ******
            context = {'object':order}
            ??????
        
        except ObjectDoesNotExist:
            messages.error(self.request, "No active order yet, sorry!!!")
            return redirect('/')


and my html
{% if object.item.paper_frame %}
  <tr>
  <td><b>{{object.get_total_everything_cover_paper}}</b></td>
</tr>
<tr>
  {% else %}
  {% if object.get_total_everything %}
  <tr>
  <td><b>N {{object.get_total_everything}}</b></td>
</tr>
<tr>
{% endif %}
{% endif %}
question from:https://stackoverflow.com/questions/65602316/if-and-else-statement-in-boolean-field

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...