1 回答

TA貢獻1862條經驗 獲得超6個贊
讓我簡化一下您看到的錯誤:
In [1]: ''.join([None])
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-1-29122f69a077> in <module>
----> 1 ''.join([None])
TypeError: sequence item 0: expected str instance, NoneType found
在您的情況下,您會在這一行得到相同的異常:
address = " ".join([self.full_address])
意思self.full_address就是None.
與此同時,我認為您不需要join那里,因為您嘗試連接單個字符串。您對這條線有何期望?
所以你的模型的代碼將如下所示:
class Profile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)
full_address = models.CharField(null=True,blank=True,max_length=128)
latitude = models.DecimalField(max_digits=9, decimal_places=6, blank=True, default='0')
longitude = models.DecimalField(max_digits=9, decimal_places=6, blank=True, default='0')
slug = models.SlugField(editable=False)
def save(self, *args,**kwargs):
self.slug = slugify(self.user.username)
if self.full_address:
geo_code = geocoder.geocode(self.full_address)
if geo_code:
self.longitude = geo_code[0]['geometry']['lng']
self.latitude = geo_code[0]['geometry']['lat']
super(Profile, self).save(*args, **kwargs)
添加回答
舉報