Python/Django Concatenate a string depending on whether that string exists
Posted
by
Douglas Meehan
on Stack Overflow
See other posts from Stack Overflow
or by Douglas Meehan
Published on 2012-07-01T21:07:51Z
Indexed on
2012/07/01
21:15 UTC
Read the original article
Hit count: 215
I'm creating a property on a Django model called "address". I want address to consist of the concatenation of a number of fields I have on my model. The problem is that not all instances of this model will have values for all of these fields. So, I want to concatenate only those fields that have values.
What is the best/most Pythonic way to do this?
Here are the relevant fields from the model:
house = models.IntegerField('House Number', null=True, blank=True)
suf = models.CharField('House Number Suffix', max_length=1, null=True, blank=True)
unit = models.CharField('Address Unit', max_length=7, null=True, blank=True)
stex = models.IntegerField('Address Extention', null=True, blank=True)
stdir = models.CharField('Street Direction', max_length=254, null=True, blank=True)
stnam = models.CharField('Street Name', max_length=30, null=True, blank=True)
stdes = models.CharField('Street Designation', max_length=3, null=True, blank=True)
stdessuf = models.CharField('Street Designation Suffix',max_length=1, null=True, blank=True)
I could just do something like this:
def _get_address(self):
return "%s %s %s %s %s %s %s %s" % (self.house, self.suf, self.unit, self.stex, self.stdir, self.stname, self.stdes, self.stdessuf)
but then there would be extra blank spaces in the result.
I could do a series of if statements and concatenate within each, but that seems ugly.
What's the best way to handle this situation?
Thanks.
© Stack Overflow or respective owner