Is there a way to get the text from a URLField?

I have a URLField in my model. In one function, I want to copy the url to do some things to it as a string. However, I get an error like this

    hurl = re.search(regex, link)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/re/__init__.py", line 177, in search
    return _compile(pattern, flags).search(string)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: expected string or bytes-like object, got 'URLField'

Is there a way to grab the text out of the URLField instead of the object itself?

You should show more of the code. I think you’re doing something like YourModel.field instead of an_instance_of_your_model.field.

what is the output of print(link) or type(link) when placed before the above line?

The link passed in above is directly from this

link1 = models.URLField(null=True, blank=True)

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.

If this is not clear to you, then I suggest you review the docs at 9. Classes — Python 3.13.0 documentation

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.

I see. That clears things up.