python - How to use filter_horizontal on a ForeignKey between two apps using ContentTypes in Django admin? -
say have app named pantry connect other app may come along. keep app decoupled, generic relations used through model linkeditem connects ingredients model apps outside pantry.
i content on other end of generic relation, app named bakery, able filter_horizontal ingredients.
pantry models.py
from django.db import models django.contrib.contenttypes.models import contenttype django.contrib.contenttypes import fields class ingredient(models.model): ''' model containing ingredients, slugs, , descriptions ''' name = models.charfield(unique=true, max_length=100) slug = models.slugfield(unique=true, max_length=100) description = models.charfield(max_length=300) # method return name of db entry def __str__(self): return self.name class linkeditem(models.model): ''' model links ingredients various other content models ''' content_type = models.foreignkey(contenttype) object_id = models.positiveintegerfield() content_object = fields.genericforeignkey('content_type', 'object_id') ingredient = models.foreignkey(ingredient) # method return name of db entry def __str__(self): return self.ingredient.name # defines options model class meta: unique_together = (('content_type','object_id')) # prevents duplicates
bakery admin.py
from django.contrib import admin bakery.models import cake class cakeadmin(admin.modeladmin): filter_horizontal = ('') # put here ingredients show up?
any ideas?
i think genericrelation
for, need add 1 cake
model , use it's name in cakeadmin.
but need use inlines if don't want lot of workaround m2m fields not supported relations intermediary models.
Comments
Post a Comment