0

I have this model:

class MsTune(models.Model):
    name = models.CharField(max_length=255) # title (source)
    start_page = models.CharField(max_length=20, blank=True, null=True, default=None)

def __str__(self):
        if not self.start_page or self.start_page != '' or self.start_page is not None or self.start_page is not "None" or self.start_page is not null:
            return '%s, %s' % (self.name, self.start_page)
        else:
            return '%s' % (self.name)

As you can see, I want only name if start_page is empty, or name, start_page if start_page is filled. No matter how many conditions I put (see above), I keep getting name, None in my template. What am I missing? Also, is there a shorter code I can put in place, instead of the verbose if / else ?

Edit

This is the content of my field in the database:

mysql> SELECT start_page from bassculture_mstune where id = 1942;
+------------+
| start_page |
+------------+
| NULL       |
+------------+
1 row in set (0.00 sec)

And in Django's shell:

>>> mytune = MsTune.objects.get(id=1942)
>>> print(mytune.start_page)
None
HBMCS
  • 686
  • 5
  • 25
  • 2
    2nd answer on https://stackoverflow.com/questions/6584235/django-want-to-display-an-empty-field-as-blank-rather-displaying-none uses the built-in default_if_none filter. – Ruperto Jan 28 '21 at 12:21
  • what do you get, when using positive condition? if self.start_page: return '%s, %s' % (self.name, self.start_page) else: return '%s' % (self.name) In fact, it'd more enlightening to show view and template. – OlegТ Jan 28 '21 at 12:44
  • The view is a simple `tunes = MsTune.objects.all()`, and the template just `{% for tune in tunes %} {{ tune }}{% endfor %}`. The positive condition shows the string "title, None" to the template. – HBMCS Jan 28 '21 at 13:12
  • I think the if statement is not as you want to have it ... "or" is the wrong logic. It should be something like ... is not "None" and ... is not "Null" ... etc. – Razenstein Jan 28 '21 at 13:26
  • Nope, I'm still getting None. Besides, why 'and'? It'll never bee all those things together. – HBMCS Jan 28 '21 at 13:33
  • If it is "None" it is not "" ... so the other "or" condition is true and it displays the None – Razenstein Jan 28 '21 at 13:36
  • Well, I put all 'and's and it's still outputting None unfortunately. – HBMCS Jan 28 '21 at 13:38
  • Also remove "Not self.start_page" at the beginning – Razenstein Jan 28 '21 at 13:41
  • No luck, I'm afraid. I still think that the 'and' is not right, since returns true only if all conditions are true, while I want to test if either is null, or empty, or none, when only one of them is true I don't want to output start_page. Anyway, with all 'and's and not 'not start_page' is still returning None. – HBMCS Jan 28 '21 at 13:52

1 Answers1

1

As default value is going to be "" as from your field, simply checking not value should work:

def __str__(self):
    returnVal = f"{self.name}"
    if self.start_page:
        returnVal = f"{returnVal}, {self.start_page}"
    return returnVal

Or, you can use ternery operation:

def __str__(self):
    return self.start_page and f"{self.name}, {self.start_page}" or f"{self.name}" 

Python ternary operation: Refs

Biplove Lamichhane
  • 3,995
  • 4
  • 14
  • 30
  • As you can see from my code above, I already have the `if self.start_page is not None` condition. My main problem is that `None` is still displayed. – HBMCS Jan 28 '21 at 12:27
  • @HBMCS edditted, have a look. – Biplove Lamichhane Jan 28 '21 at 12:34
  • That's also what I tried as first thing. I've added it to the loooong list of 'ifs'. Still no luck. Can it be how the data has been written in the MySQL database? I've imported it from a .sql dump. What other kind of null/None/'none' can there be? – HBMCS Jan 28 '21 at 12:40
  • @HBMCS can you try: `print(self.start_page)` and check what is the output. Both in template and in terminal/ – Biplove Lamichhane Jan 28 '21 at 12:41