0

In my Order model, I have two field Diagnosis_note and Internal_memo, if one of the fields is blank, I want to display a red button and a green button if both are blank. However, currently in my function, if Diagnosis_note or both fields are blank, it displays a green button and if Internal_memo field is blank is displays a red button. It should display a red button if either one of it is blank or not blank and green if both are blank. Something is wrong with my is_complete function, but I can't figure out what.


class Order(models.Model):
    Diagnosis_note = models.CharField(max_length=30, blank=True)
    Internal_memo = models.CharField(max_length=100, blank=True)

    def is_complete(self):
        fields_list = [self.Diagnosis_note, self.Internal_memo]

        if self.Diagnosis_note or self.Internal_memo is None:
            return True
        else:
            return False


html
{% if Order.is_complete %}

      <td><a href="{% url 'accounts:special-notes' Order.id %}" class="btn btn-danger" role="button"><i class="fas fa-check-circle"></i></a></td>

      {% else %}
      <td><a class="btn btn-success" role="button"><i class="far fa-circle"></i> </a></td>
        {% endif %}
1
  • I think there is a misunderstanding of the precedence in your condition. self.Diagnosis_note or self.Internal_memo is None means "Diagnosis_note is considered True or Internal_memo is None", not "one of Diagnosis_note and Internal_memo is None". Commented Aug 19, 2020 at 9:11

4 Answers 4

1

You compare self.Internal_memo is None but it can be a blank string (not None).

def is_complete(self):
    return self.Diagnosis_note and self.Internal_memo  # returns True if both fields are not False and not empty. otherwise, it returns False
Sign up to request clarification or add additional context in comments.

Comments

0

You need to rewrite your if-else statement in the model. What you want is green only when both are blank, correct ? Red if either one is not blank or both are not blank. And from your html, both are blank, then return False, else True. This will replace your whole if else statement.

return self.Diagnosis_note or self.Internal_memo

5 Comments

do I have to replace return true with this ?
Replace your whole if-else statement with that, assuming you want the conditions listed in my answer.
problem is it's doing the opposite . It's returning green if both fields are not blank and red if both fields are blank or either one is blank.
Sorry, I fixed the answer.
I want the function to return red if both fields are not blank or either one is not blank and green if both fields are blank.
0

Try changing the line if self.Diagnosis_note or self.Internal_memo is None: to if self.Diagnosis_note == None or self.Internal_memo == None:

Test for w = None and q = 1:

[1]if q or w is None: print("Y")
>>> Y
[2] if w or q is None: print("Y")
>>>
[3] if w == None or q == None: print("Y")
>>> Y

Comments

0

First, you are not dealing with an object, instead you are calling a Class. You have to first get the instance you are looking for example:

order = Order.objects.get(id=order_id)

Then pass the order to the template and do the check.

Second, when it comes to your if condition in is_complete() method, it has to be like this

if not self.Diagnosis_note and not self.Internal_memo: return False
else: return True

Conditions must be there for both the attributes and don't use None since what if the value is ''. not operator handles both None and '' in this case.

2 Comments

I have replaced " if self.Diagnosis_note or self.Internal_memo is None:" with "if not self.Diagnosis_note or not self.Internal_memo:" , but now it returns green if both fields are not blank and red if both fields are blank or either one is blank
My bad, it should be... if not self.Diagnosis_note and not self.Internal_memo: return False. Edited the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.