[Answer]-Python strip newlines when saving a Django Model does not work

1👍

When you do:

def save(self, *args, **kwargs):
    self.title.replace("\r\n", "")

Python removes any carriage return+linefeed pairs and then throws the result away.

def save(self, *args, **kwargs):
    self.title = self.title.replace("\r\n", "")

will work, or you might even just do:

def save(self, *args, **kwargs):
    self.title = self.title.rstrip()

if you only wanted to remove trailing newlines.

👤Duncan

Leave a comment