문제

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!

도움이 되었습니까?

해결책

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

다른 팁

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top