skip to Main Content

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}

2

Answers


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

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

    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)
    
    Login or Signup to reply.
  2. Create a property function in your profile model.

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

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

    profile_instance.get_telegram_link()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search