Kindly asking to help me with this: How to properly design it ? Which inheritance type to use ? How would you do it ?
I have a model Link
where I want to save all links which will then be used in application.
There are several types of Link
:
- external link which is URL (for example: “https://example.com”)
- internal link represented by view (for example: “myapp.index_page”)
- relative link (for example: “…/contact-me”)
- etc.
Each of this links needs different href
represantion (different HTML code).
My idea is to have a parent model Link
and 3 child models: ExternalLink
, InternalLink
, RelativeLink
.
When creating links, I should be able to separately create only child model instances.
When using links, I don’t care which type of link it is, so I would like to do ForeignKey
to parent model to see all instances together.
class Link(models.Model):
text = models.CharField(max_length=100)
icon = models.CharField(...)
class ExternalLink(Link):
url = URLField()
class InternalLink(Link):
view = models.CharField(choices=...)
class RelativeLink(Link):
address = models.CharField(...)
class Page(models.Model):
text = models.CharField(max_length=100)
link = models.ForeignKey(Link)
Regarding HTML represantion, ideally each child would have its own template and would render itself depending on the link type.