Shared Relationships / Shared Foreign Keys in Django Admin

I am trying to show a shared foreign key in a Django admin, but cannot make it work. I have a FullCitation class that can be associated with either the NonSupportedProgram class or the SupportedProgram class. So, I used a shared foreign key.

In admin, I want users to be able to select only “NonSupportedProgram” or “SupportedProgram” from the content_type drop-down list, and then from the object_id field I need users to be able to select existing NonSuportedPrograms or existing supported programs from the drop-down list, with the possibility of creating a new one. Is it possible? Where am I mistaken?

models.py

class FullCitation(models.Model) # the software to which this citation belongs # either a supported software program or a non-supported software program limit = models.Q(app_label = 'myprograms', model = 'supportedprogram') | models.Q(app_label = 'myprograms', model = 'nonsupportedprogram') content_type = models.ForeignKey(ContentType), limit_choices_to = limit, ) object_id = models.PositiveIntegerField() content_object = generic.GenericForeignKey('content_type', 'object_id') is_primary = models.BooleanField(help_text="Is this the Primary Citation for the software program?") class Meta: unique_together = ('content_type', 'object_id') app_label = 'myprograms' reversion.register(FullCitation) class NonSupportedProgram(models.Model): title = models.CharField(max_length=256, blank = True) full_citation = generic.GenericRelation('FullCitation') class Meta: app_label = 'myprograms' reversion.register(NonSBGridProgram) class SupportedProgram(models.Model): title = models.CharField(max_length=256, blank = True) full_citation = generic.GenericRelation('FullCitation') # and a bunch of other fields..... 

admin.py

 class FullCitationAdmin(reversion.VersionAdmin): fieldsets = ( ('Which Program', { 'fields': ('content_type', 'object_id', ), }), ('Citation Information', { 'fields': ('is_primary',), }),) # autocomplete_lookup_fields = { # 'generic': [['content_type', 'object_id']], # } # inlines = ['NonSupportedProgramInline', ] list_display = ('content_object', 'is_primary',) search_fields = ('content_object__title', ) # list_filter = ('content_object',) 
+7
python django django-models django-admin generic-foreign-key
source share
1 answer

This is the module that displays GenericForeignKeys in Django Admin:

https://github.com/lexich/genericrelationview

It just doesn't work with jQuery customization without conflict (e.g. with Django CMS).

0
source share

All Articles