gpt4 book ai didi

python - Django 管理员 : automatically generate multiple inlines with the same model

转载 作者:太空狗 更新时间:2023-10-30 01:29:57 25 4
gpt4 key购买 nike

我正在编写我的第一个 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”值?

我用谷歌搜索了一下,一无所获 =)

附加:好的。我修改了“信息”类:

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 那样创建代理。它每次都会返回相同的对象。

最佳答案

我找到了解决方案。现在我可以即时创建动态代理模型。模型.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

关于python - Django 管理员 : automatically generate multiple inlines with the same model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11896813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com