That’s the only relevant line of code to the function where this error is occurring. The function is defined within the class of the model that link belongs to.
I don’t know why you are so hostile to that simple reply. You don’t have to be. This is not Stack Overflow.
There is really not any other code besides the class declaration and the function declaration under that sole URLField named link. All it does is try to regex the link.
Here is the simplified code since it makes you mad
class Simp(models.Model):
link1 = models.URLField(null=True, blank=True)
def link_part_extract(link):
if link:
link_host_regex # definition omitted for brevity
hurl = re.search(link_host_regex, link)
if hurl:
return hurl.group(1)
return ""
If you declare a function as a member of a class in python you should name the first argument self. You don’t here, which seems like a quite bad mistake. The correct code should be:
class Simp(models.Model):
link1 = models.URLField(null=True, blank=True)
def link_part_extract(self):
if self.link1:
link_host_regex # definition omitted for brevity
hurl = re.search(link_host_regex, self.link1)
if hurl:
return hurl.group(1)
return ""
As you can clearly see you did NOT initially show us the relevant code. As I said. You should listen to people trying to help you.
Nope. That is not the correct code. In the event I need to add another link the model, I can’t pass in the link field with your code, so then that function would need to be duplicated. Quite a bad mistake.
I’m simply asking if there is a way to get the string out of a URLField, not to rewrite the functionality of my code to match someone else’s. Again, this is not Stack Overflow. You don’t have to do that here. You should listen to what people are asking.
Ok, then in that case you need to move that function out of the class. In any case the code you showed initially is very wrong. You are missing some basic knowledge of how Python works.
What I think you might be missing here is that the field is a definition in a class. However, the string in the field is data within an instance of that class.
There is no “string” to get from the field. There is only a string within the field in an instance of that class.
The class is just a definition of the data it will contain. It does not contain the data itself.
A more complete statement of what you’re trying to do with this, along with the example of how you’re trying to use this function may go a long way to clearing up the confusion.