возник такой вопрос:
есть некие простые модели:
from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from django.utils.translation import get_language, ugettext_lazy as _
#from satchmo.product.models import Category
class OptionsManager(models.Manager):
def get_products(self, **kwargs):
products = self.filter(**kwargs)
return products
def get_opt_variants(self,variant):
variants = []
prs = self.all()
for p in prs:
if getattr(p,variant) not in variants:
variants.append(getattr(p,variant))
return variants
class tCategoryManager(models.Manager):
pass
class tProductManager(models.Manager):
pass
class tCategory(models.Model):
name = models.CharField(_("Name"), max_length=200)
product_options_ctype = models.ForeignKey(ContentType)
objects = tCategoryManager()
def _get_pr_option(self):
pots = self.product_options_ctype.model_class()
return pots
product_options = property(_get_pr_option)
def __unicode__(self):
return self.name
class tProduct(models.Model):
name = models.CharField(_("Name"), max_length=20, blank=True)
category = models.ManyToManyField(tCategory, blank=True, verbose_name=_("Category"))
product_options_ctype = models.ForeignKey(ContentType)
objects = tProductManager()
def save(self):
pass
product_option = property(_get_pr_option)
def __unicode__(self):
return self.name
class VideoProductOptions (models.Model):
"""diff options for video category (computers) """
name = models.CharField(_("Name"), max_length=20, default="Videocards options")
producer = models.CharField(_("Producer"), max_length=20, blank=True)
gpu = models.CharField(_("GPU"), max_length=20, blank=True)
mem_type = models.CharField(_("Memory Type"), max_length=10, blank=True)
mem_amount = models.CharField(_("Memory Size"), max_length=10, blank=True)
vo = models.CharField(_("Videoout"), max_length=10, blank=True)
mem_bus = models.IntegerField(_("Memory Bus"), max_length=20, default="0")
interface = models.CharField(_("Interface"), max_length=10, blank=True)
content_type = models.ForeignKey(ContentType)
object_id = models.IntegerField()
content_object = generic.GenericForeignKey()
objects = OptionsManager()
def __unicode__(self):
return self.name
from django.contrib import admin
from django.contrib.contenttypes.generic import GenericTabularInline
from emall.site.models import VideoProductOptions, tCategory, tProduct
class VideoInline(GenericTabularInline):
model = VideoProductOptions
extra = 1
# ct_field_name = 'content_type'
# id_field_name = 'object_id'
class ProductOptions(admin.ModelAdmin):
inlines = [VideoInline]
admin.site.register(VideoProductOptions)
admin.site.register(tCategory)
admin.site.register(tProduct, ProductOptions)
Причем как вы поняли из кода админки - VideoProductOptions будет инлайном при tProduct.
Возможно ли реализовать такой бред?
(я понимаю что при сохранении инстансов VideoProductOptions они будут иметь content_object равным tProduct"у с которым они были сохранены, но, к сожалению, при дженерик релейшенах не работаeт VideoProductOptions.objects.(filter|get)(content_object=…) )