views:

134

answers:

1

Since I have not found a Related Links app that works with Django 1.0/trunk, I was looking to create my own.

I would like to attach "Related Links" to models in the same generic way that Comments framework or Tags work.

I've looked over the Content Types documentation but can't wrap my head around (nor find much documentation for) how to use Generic inline formsets - which is what I'm pretty sure I have to use, but correct me if I'm wrong.

My specific requirement is to be able to relate these "Related Links" to almost any model, and to have the form available outside of the Admin - I'll have logged in members of a certain role adding these links, in my specific case.

I thought about tearing into the source of the Comments app, but I know that it uses special template tags, etc, and I'm just not sure if that'd be overkill for this task.

Looking for links, extra documentation, and possibly even examples of using the generic inline formsets (in Generic Views), or solving the problem in a different way if I'm approaching it wrong.

EDIT: I've used James Bennett's example of Generic Inlines to construct and successfully use those Related Links in the Admin. So the real question is: How do I use James' Related Links outside of the Admin?

+1  A: 

You can use django.contrib.contenttypes.generic.generic_inlineformset_factory for that. It has the same interface as inlineformset_factory (with 2 additional parameters: ct_field and fk_field, they can be used to specify your model's contenttype's related field names instead of inlineformset_factory's fk_name).

Documentation for inlineformset_factory can be found here:

http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#inline-formsets

Documentation for formsets is also useful.

Mike Korobov
I did something along these lines, and created some Custom Template Tags to display the form and links related to a given object, so the tags look something like: {% display_generic_related_links_for object %}
anonymous coward