Writing custom url model field in Django

I am creating one model called Profile in which i have to add a url field for telegram profile. It takes username as input, but in output it should look like this: https://t.me/{username}. How I can create custom model field for this problem. Thanks by the way

My expectation is that, you only enter username, but in output it comes with prefix https://t.me/ and full url path will be https://t.me/{username}

create a function under your model and use it in template:

def get_tg_link(self):

or override save function under your model

def save(self, *args, **kwargs):
    self.telegram_profile = f'https://t.me/{self.tg_username}'
    super().save(*args, **kwargs)

Create a property function in your profile model.

def get_telegram_link(self):
    return f"https://t.me/{self.telegram_username}"

When accessing the model link on your template or code use:

Back to Top