我正在编写我的第一个Django应用程序。我有以下数据库模型:
class Person(models.Model):
first_name = models.CharField(max_length = 100)
last_name = models.CharField(max_length = 100)
class InformationType(models.Model):
name = models.CharField(max_length = 100)
class Information(models.Model):
person = models.ForeignKey(Person)
info_type = models.ForeignKey(InformationType)
info = models.CharField(max_length = 200)
我想通过按类型拆分信息模型并动态地创建Django Admin(类PersonAdmin(ModelAdmin))中的多个内联。另外,我想从用户界面隐藏(排除)字段'info_type',并自动用相应的值填充它。
我可以使用'info_type'过滤的'信息'数据动态创建内联,但在UI中隐藏此字段会使其在保存时为空。
我该怎么办?是否可以制作隐藏的字段?或者我应该在哪里存储'info_type'值?
我用Google搜索并没有发现任何东西=)
追加: 好。我修改了'信息'类:
class Information(models.Model):
def save(self, *args, **kwargs):
self.info_type = self.fixed_info_type
super(Information, self).save(*args, **kwargs)
...并且制作了一些代理:
class InformationManager(models.Manager):
def __init__(self, info_type, *args, **kwargs):
self.__info_type = info_type
super(InformationManager, self).__init__(*args, **kwargs)
def get_query_set(self, *args, **kwargs):
return super(self.__class__, self).get_query_set(*args, **kwargs).filter(info_type=self.__info_type)
class PhoneInformation(Information):
fixed_info_type = 'PHONE'
objects = InformationManager(fixed_info_type)
class Meta:
proxy = True
class EmailInformation(Information):
fixed_info_type = 'EMAIL'
objects = InformationManager(fixed_info_type)
class Meta:
proxy = True
在admin.py中:
from contacts.models import Person, PhoneInformation, EmailInformation
class PersonAdmin(admin.ModelAdmin):
__inlines = []
class classproperty(property):
def __get__(self, instance, owner):
return super(self.__class__, self).fget.__get__(None, owner)()
@classproperty
@classmethod
def inlines(cls):
def get_inline(InformationModel):
class Inline(admin.TabularInline):
model = InformationModel
exclude= ['info_type']
return Inline
if not cls.__inlines:
for InformationModel in [PhoneInformation, EmailInformation]:
cls.__inlines.append(get_inline(InformationModel))
return cls.__inlines
所以它看起来很难看,并且不符合DRY原则。我无法以与InlineAdmin相同的方式创建代理。它每次都会返回相同的对象。
答案 0 :(得分:1)
我过去做过的事情是:
创建各种内联表单,覆盖每个表单的查询集,只允许按类型信息的特定子集
(看看灵感:Limit the queryset of entries displayed for a django admin Inline)
答案 1 :(得分:1)
我找到了解决方案。现在我可以动态创建动态代理模型。 models.py:
class Person(models.Model):
first_name = models.CharField(max_length = 100)
last_name = models.CharField(max_length = 100)
class InformationType(models.Model):
class Meta:
ordering = ['order']
name = models.CharField(max_length = 200)
order = models.IntegerField()
class Information(models.Model):
person = models.ForeignKey(Person)
info_type = models.ForeignKey(InformationType)
value = models.CharField(max_length = 200)
@classmethod
def getProxy(cls, info_type):
class InformationManager(models.Manager):
def get_query_set(self, *args, **kwargs):
return super(self.__class__, self).get_query_set(*args, **kwargs).filter(info_type = info_type)
def save(self, *args, **kwargs):
self.info_type = info_type
super(Information, self).save(*args, **kwargs)
class Meta:
proxy = True
return type(
'Information'+str(info_type.pk),
(Information,),
{
'Meta': Meta,
'save': save,
'__module__': 'contacts.models',
'objects': InformationManager(),
}
)
在admin.py中:
class PersonAdmin(admin.ModelAdmin):
__inlines = []
class classproperty(property):
def __get__(self, instance, owner):
return super(self.__class__, self).fget.__get__(None, owner)()
@classproperty
@classmethod
def inlines(cls):
def get_inline(info_model):
class Inline(admin.TabularInline):
model = info_model
exclude= ['info_type']
return Inline
if not cls.__inlines:
for info_model in [Information.getProxy(info_type) for info_type in InformationType.objects.all()]:
cls.__inlines.append(get_inline(info_model))
return cls.__inlines