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 can make filter_horizontal show linkeditem's admin in django. 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.manytomanyfield(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?
a solution create generictabularinline linkeditem , putting restrictions on display avoid duplicates below:
from django.contrib.contenttypes.admin import generictabularinline class linkeditemadmin(generictabularinline): model = linkeditem # choosing field , display field = ['ingredient'] filter_horizontal = ['ingredient'] # these removing potential issues in admin = 0 min_num = 1 max_num = 1 can_delete = false
then in cakeadmin can make ingredients show up.
class cakeadmin(admin.modeladmin): inlines = [linkeditemadmin]
Comments
Post a Comment