Package: django-generic-links
django-generic-linksSimple app to attach links to any Django model. Compatible with Django 4.x to 5.0 and Python 3.9 to 3.12.FeaturesModel for creating generic link relationsReverse Generic Relation (Django) for your modelsModel AdminGeneric inline admin to manage any model's generic linksA template tag to get all links for a given model instanceA fully working example projectInstallationViapipcommand:pipinstalldjango-generic-links...or you can clone the repo and install it usingpiptoo:gitclonegit://github.com/matagus/django-generic-links.gitcddjango-generic-links
pipinstall-e.then addgeneric_linksto yoursettings.py:INSTALLED_APPS=(# ..."generic_links",)then run the migrations:pythonmanage.pymigrateand finally add the reverse generic relation to each of the models you're going to add generic links to:fromdjango.dbimportmodelsfromdjango.contrib.contenttypes.fieldsimportGenericRelationclassArtist(models.Model):name=models.CharField(max_length=100)bio=models.TextField()# This is important so that we can get the GenericLink related instances for an object of this modelgeneric_links=GenericRelation("generic_links.GenericLink")classAlbum(models.Model):title=models.CharField(max_length=100)artist=models.ForeignKey(Artist,on_delete=models.CASCADE)release_date=models.DateField(null=True,blank=True)generic_links=GenericRelation("generic_links.GenericLink")UsageUsing django-generic-links models>>>fromgeneric_links.modelsimportGenericLink>>>frommusic.modelsimportArtist>>>>>># Get an artist from the database...>>>lou_reed=Artist.objects.get(pk=1)>>>lou_reed<Artist:LouReed>>>>>>># Create the first link>>>link1=GenericLink(title="Wikipedia Page",url="http://en.wikipedia.org/wiki/Lou_Reed",content_object=lou_reed)>>>link1.save()>>>>>># and then a second one>>>link2=GenericLink(title="Encyclopaedia Britannica",url="https://www.britannica.com/biography/Lou-Reed",content_object=lou_reed)>>>link2.save()>>>>>># Now get all the links for the artist object:>>>lou_reed.generic_links.all()<QuerySet[<GenericLink:EncyclopaediaBritannica::https://www.britannica.com/biography/Lou-Reed>,<GenericLink:WikipediaPage::https://en.wikipedia.org/wiki/Lou_Reed>]>Generic Links Inline AdminSince aGenericLinkinstance will be associated to another object you usually
wish to show an inline model admin form in that model form.fromdjango.contribimportadminfromgeneric_links.adminimportGenericLinkStackedInlinefrommusic_app.modelsimportArtist@admin.register(Artist)classArtistAdmin(admin.ModelAdmin):# ...inlines=[GenericLinkStackedInline]Using django-generic-links templatetagsNow imagine you have an artist page. You're passingartistobject using template
context and you want to get all the links for it:{% load generic_links_tags %}<hl>{{ artist.name }}</hl><p>{{ artist.description }}</p><h2>Links for {{ artist.name }}</h2>{% get_links_for artist as artist_links %}<ul>{% for link in artist_links %}
  ‹li><ahref="{{ link.url }}"title="{{ link.title }}">{{ link. title }}</a></li>{% endfor %}</ul>ContributingContributions are welcome! ❤️Please readContributing.mdfor detailed instructions on how to help.Running Testshatch run test:testwill run the tests in every Python + Django versions combination.hatch run test.py3.12-5.0:test will run them for python 3.12 and Django 5.0. Please see possible combinations usinghatch env show` ("test" matrix).Licensedjango-generic-linksis released under an BSD License - see theLICENSEfile
for more information.AcknowledgementsDevelop & built usingTools used in building this package:Cookiecutterandcookiecutter-djangopackagefor rendering this package.Posts I learned from:Switching to HatchAutomate Hatch Publish with GitHub Actions