Django admin widget GenericForeignKey

I am creating a Django application where all models can be connected to each other in the order specified by the user. I customize all this using GenericForeignKeys. A kicker is what I need to be able to support multiple collections of these types of / admin relationships. Thus, a single object may have more than one set of related objects.

Does anyone know a good GenericForeignKey widget for this situation? Preferably, it will be an autocomplete search that fills out the administrative form, as I can end up with a large number of objects.

Here is the code for my application to better understand what I mean.

from django.contrib import admin from django.contrib.contenttypes import generic from django.contrib.contenttypes.models import ContentType from django.db import models from django import forms # Models class Base(models.Model): title = models.CharField(max_length=255) class Meta: abstract = True def __unicode__(self): return self.title class Related(Base): """ A generic relationship model for relating resources. """ order = models.IntegerField(blank=True, null=True) limit = models.Q(model = 'Apple') | models.Q(model = 'Orange') | models.Q(model = 'Pear') content_type = models.ForeignKey(ContentType, related_name="related_%(class)s") object_id = models.PositiveIntegerField(db_index=True) object = generic.GenericForeignKey() related_content_type = models.ForeignKey(ContentType, related_name="related_related_%(class)s", limit_choices_to = limit) related_object_id = models.PositiveIntegerField(db_index=True) related_object = generic.GenericForeignKey('related_content_type', 'related_object_id') class Meta: ordering = ('order',) abstract = True def __unicode__(self): return self.object.title class FreshFruit(Related): pass class OldFruit(Related): pass class Apple(Base): pass class Orange(Base): pass class Pear(Base): pass # Admin classes class FreshFruitInline(generic.GenericStackedInline): model = FreshFruit extra = 1 # Admin classes class OldFruitInline(generic.GenericStackedInline): model = OldFruit extra = 1 class AppleAdmin(admin.ModelAdmin): inlines = [FreshFruitInline, OldFruitInline,] admin.site.register(Apple, AppleAdmin) class OrangeAdmin(admin.ModelAdmin): inlines = [FreshFruitInline, OldFruitInline,] admin.site.register(Orange, OrangeAdmin) class PearAdmin(admin.ModelAdmin): inlines = [FreshFruitInline, OldFruitInline,] admin.site.register(Pear, PearAdmin) 

I searched and searched and found widgets that do this for ManyToMany relationships, but nothing for my situation.

Thanks for taking the time to look at this.

+4
source share
1 answer

Look at the generic Grappelli foreign key widget that works well: django-grappelli / generic_2_2

+6
source

All Articles