7

Próbowałem wyświetlić ogólny klucz obcy w administratorze Django, ale nie mogę go uruchomić. Mam klasę FullCitation, która może być połączona z klasą NonSupportedProgram lub SupportedProgram. Więc użyłem ogólnego klucza obcego.Generic Relations/Generic Foreign Keys w Django Admin

W panelu administracyjnym chcę, aby użytkownicy mogli wybrać tylko "Nieobsługiwany program" lub "Obsługiwany program" z listy rozwijanej content_type, a następnie, z pola id_obiektu, potrzebuję, aby użytkownicy mogli wybrać z listy rozwijanej listę istniejących NonSuportedPrograms lub istniejące Obsługiwane programy, z opcją tworzenia nowego. czy to możliwe? Gdzie się mylę?

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',) 
+0

Czy kiedykolwiek to naprawiliście? – Sardathrion

+0

Nie, nigdy się nie domyśliłem. Musiałam zatrzymać to podejście i zrobić coś zupełnie innego. Nie dokładnie to, co chciałem, ale no cóż. – steph

+0

Bummer. Dziękuję Ci. – Sardathrion

Odpowiedz