Question

So this is a little exercise in the meta meta.. I want to be able to store model reference as a row in a table associated with another model. Something like this:

class Widget(models.Model):
  related = models.Model() # data model associated with this widget
  identifier = models.CharField(max_length=500) # human-friendly descriptor

This doesn't validate.. I've found an acceptable workaround, but I'm wondering if there's a more proper/graceful way of doing this.

Thanks, django wizards!

Was it helpful?

Solution

If I understand your question correctly then GenericForeignKey is what you need. Have you looked at it?

OTHER TIPS

If you just want to hold the actual model of another object, you can simply use a foreignkey to a content type:

from django.contrib.contenttypes.models import ContentType
class Widget(models.Model):
    related = models.ForeignKey(ContentType)
    identifier = models.CharField(max_length=500) # human-friendly descriptor
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top