Django admin GenericForeignKey widget

非 Y 不嫁゛ 提交于 2019-12-22 07:55:14

问题


I'm creating a Django app where all the models can be related to each other in an order set by the user. I'm setting all this up using GenericForeignKeys. The kicker is that I need to be able to support multiple collections of these types of relationship/admin. So one object can have a more than one collection of related objects.

Does anyone know of a good GenericForeignKey widget for this situation? Preferably, it would be an autocomplete search that populates the admin form since I can end up having a large number of objects.

Here is the code for my app to get a better idea of 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've searched and searched, and found widgets that do this for a ManyToMany relationship, but nothing for my situation.

Thanks for taking the time to look at this.


回答1:


Have a look at Grappelli's generic foreign key widget, which works well: django-grappelli/generic_2_2



来源:https://stackoverflow.com/questions/6756747/django-admin-genericforeignkey-widget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!