我有一个网站模型,该模型需要一个域名输入
class Website(models.Model):
domain_name = models.URLField(unique=True)
sub_domain =models.CharField(max_length=56, blank=True, default='')
suffix = models.CharField(max_length=56, blank=True, default='')
然后,我尝试使用tldextract拆分域部分,这很好用,在用户进入网站时,我想存储sub_domain,域和后缀,因此我尝试了很多方法,但是似乎无法获取存储的单个值
def __str__(self):
return self.name
def validate_and_split_domain(self):
domain = self.domain_name.lower() # make all lower case because django isn't case sensitive
values = list(tldextract.extract(domain)) #Split values into a list
self.sub_domain, self.domain_name, self.suffix = values[0], values[1], values[2]
def save(self, *args, **kwargs):
self.validate_and_split_domain()
super(Website, self).save(*args, **kwargs)