Say I have a Post object that can contain images, videos, and other types of media. I can use GenericForeignKey to bind them together. Something like:
class Post(models.Model): title = models.CharField(...) text = models.TextField(...) class AudioMedia(models.Model): ... class VideoMedia(models.Model): ... class ImageMedia(models.Model): ... class MediaObject(models.Model): post = models.ForeignKey(Post) order = models.IntegerField() content_type_media = models.ForeignKey( ContentType, limit_choices_to={ 'model__in': ( 'audiomedia', 'imagemedia', 'videomedia') }) object_id_media = models.PositiveIntegerField() obj = generic.GenericForeignKey('content_type_media', 'object_id_media')
Now I can easily create an admin interface, for example:
class MediaObjectAdminInLine(admin.StackedInline): model = MediaObject ct_field = "content_type_media" ct_fk_field = "object_id_media" extra = 0 class PostAdmin(admin.ModelAdmin): inlines = [MediaObjectAdminInLine]
Now the question is :) In admin /, I can easily create a new Mail. I can easily add another MediaObject to the message. In the panel, I have a drop-down menu to select the type (audio, video, ...), but I need to manually enter the identifier of the object that I want to associate with the message.
I tried various extensions, including grappelli. Some of them provide the ability to search for an object identifier for a link here. I want the ability to add objects here, for example, add AudioMedia, VideoMedia, ImageMedia, depending on what I choose from the drop-down list.
Any suggestions?
django django-models django-admin
magiambelli
source share