Issue
I need to model a postal address that can have multiline street address, the city, the state (province), and the postal code. the country is omitted.
I need to preserve line breaks in the street addresses but still be able to search the addresses.
I see two ways to do it:
class Address(models.Model):
street = models.ForeignKey('StreetAddress')
city = models.TextField()
province = models.TextField()
code = models.TextField()<br>
class StreetAddress(models.Model):
line_number = models.IntegerField()
text = models.TextField()
or this one which stores the street address in a single text field but uses special separator characters to encode line breaks:
class Address(models.Model):
street = models.TextField()
city = models.TextField()
province = models.TextField()
code = models.TextField()
what is the best way to do it in terms of code readability and efficiency (or their balance)?
Solution
Unless the majority of your addresses have multi-line street parts (and have many lines), I'd go for the latter, storing it all in a single field, and not bothering with an additional model. If most of your multi-line addresses are only two lines, consider creating a street and street2 field in your Address model (you could choose more descriptive names for these two "street" fields). The first would store the first street line, and the second field would store all additional lines (separated by newlines). I would think when searching addresses, you'd most often search on the address line that contains the street number, so maybe in your program logic you'd ensure that the street number line was always stored in the first "street" field, which you could then add an index on in your database.
On the other hand, if most of your addresses will have multi-line street parts, and have more than two lines, then it makes sense to create that second model.
If you don't know in advance, and don't mind potentially "migrating" in the future, go for the simpler model. Otherwise, go for your two-model design.
Answered By - imm
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.