Overriding Save in Model

9 views
Skip to first unread message

cale...@gmail.com

unread,
Jan 16, 2019, 11:41:19 AM1/16/19
to Django users
I'm trying to extract an entered domain name and split it up so that i can store unique domains in a specific table
The flow
user will enter website address > model takes website address > splits it into sub_domain, domain and suffix and stores the values in the appropriate split fields

so far my extraction works, i can get the values into a list and assign the values to the appropriate variables, using the shell i'm adding new sites but and the domain is going into the domain_name field but i am not getting the data split

I'm only two weeks into learning python and Django and i love it but this has stopped me in my tracks for now, any help appreciated

class Website(models.Model):
name = models.CharField(max_length=100, default=' ')
description = models.TextField(max_length=2000, blank=True)
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='')
is_secure = models.BooleanField(null=True)
logo = models.CharField(max_length=256, default="placeholder.png")
type = models.CharField(choices=WEBSITE_TYPES, default='O', max_length=15)

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))
self.sub_domain, self.domain_name, self.suffix = values[0], values[1], values[2]

def save(self, *args, **kwargs):
Website.validate_and_split_domain()
super(Website, self).save(*args, **kwargs)

Matthew Pava

unread,
Jan 16, 2019, 11:55:42 AM1/16/19
to django...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/94d5ffbb-8975-471a-b9bb-30870e893ee8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

cale...@gmail.com

unread,
Jan 16, 2019, 12:07:18 PM1/16/19
to Django users
thanks,  i've got tldextract which is sufficient for splitting the domain up

the problem is I cannot save the individual parts and only end up saving the domain name

To post to this group, send email to djang...@googlegroups.com.

Matthew Pava

unread,
Jan 16, 2019, 12:21:24 PM1/16/19
to django...@googlegroups.com

You’re converting an object (ExtractResult) into a list.  That’s just weird.

 

        ext = tldextract.extract(domain)

        self.sub_domain = ext.subdomain

        self.domain = ext.domain

        self.suffix = ext.suffix

 

Saying that, I would rather use the built-in, Pythonic way of parsing URLs.

To post to this group, send email to django...@googlegroups.com.

cale...@gmail.com

unread,
Jan 16, 2019, 1:34:57 PM1/16/19
to Django users
Thanks, I did say i was only two weeks into learning python and Django.

I'm sure in a few months, maybe weeks or even days, i'll look back at my code and say wtf
Reply all
Reply to author
Forward
0 new messages