动态django表单 - 变量字段 [英] Dynamic django forms - Variable fields
问题描述
这些文章类型启用了一些vocab,有些没有,例如:图像可以用术语来自A,B以及一个来自A或C的新闻文章。
然而,这只存在于数据库级别。
我想要做的是输出字段,具体取决于是否为内容类型启用了某个内容类型,例如:
-
确定此内容类型是否连接到词汇表。
-
加载词汇表
-
输出CharField或选择每个词汇表。
我最后一步的努力所以我希望有人能指出我正确的方向。
清楚我使用的代码,所以这里是:
词汇
- 标签
NewsArticle可以使用标签f rom vocab 1,3
博客可以使用来自vocab的标签1,2
此代码是为内容类型启用的词汇表:
def get_vocabularies(self,type = None):
vocabularies = {}
object = namedtuple(Vocabulary vid name desc help relationship hierarchy multiple req tags weight)
if(type!= None):
vocabs = VocabularyTypes.objects.filter(type = type)
在vocab中的vocab:
v = Vocabulary.objects.get(pk = vocab.vid)
词汇表[v.vid] = object(vid = v.vid,
name = v.name,
desc = v.desc,
help = v.help,
relations = v.relations,
hierarchy = v.hierarchy,
multiple = v.multiple,
req = v.req,
tags = v.tags,
weight = v.weight
)
else:
vocabs = Vocabulary.objects.all()
for v in vocab:
vocab = Vocabulary.objects.get(pk = v.vid)
vocabularies [v.vid] = object(vid = vocab.vid,
name = vocab.name,
desc = vocab.desc,
help = vocab.help,
relations = vocab.relations,
hierarchy = vocab.hierarchy,
multiple = vocab.multiple,
req = vocab.req,
tags = vocab.tags,
weight = vocab.weight
)
返回词汇
所以我已经尝试过的代码是这样的:
def free_tags(self,type):
$ p但是,当在视图中使用它时,它打印出内部的python标识符(对象意图在...等等)。
vocabs = Label.objects .get_vocabularies(type)
for vid in vocabs.keys():
output = forms.CharField(label = vocabs [vid] .name,
required = vocabs [vid ] .req,
widget = forms.TextInput(attrs = {'size':64})
)
返回输出
$ b $另一个问题是,这些字段没有名称,即它们都被称为输出。
所以我需要知道的是输出如何场数,当时的数量ds是未知的。
解决方案本文帮助我很多开始使用动态表单。
最好的方式(我认为)是这样的:你有创建你的表单类的功能,例如
def make_form(instance):
fields = {'a':forms.CharField(max_length = 50),
'b':forms.CharField(widget = forms.Textarea)}
如果some_condition:
fields [ 'additional'] = forms.CharField()
返回类型('SomeForm',(forms.BaseForm,),{'base_fields':fields})
您只需在字典中定义一些字段,只有满足条件才能设置某些字段(
instance
一些你想要测试条件的模型实例),并用Python的type()
功能。I have some models; Vocabularies (tags lists), Labels (tags) and different articles types.
These article types have some vocabs enabled, and some not, for instance: Image can tagged with terms from A, B and a news article with terms from A or C.
This however only exists at the database level.
What i'm trying to do is to output fields depending on if a vocab is enabled for a content type or not, like this:
Determine if this content type is connected to a vocabulary.
Load the vocabularies
output a CharField or Select for each vocabulary.
All my efforts for the last step have failed however, so I'm hoping someone can point me in the right direction.
My apologies for not being very clear about the code I am using, so here it is:
Vocabulary - Tags
NewsArticle can use tags from vocab 1, 3 Blog can use tags from vocab 1,2
This code get's the vocabularies which are enabled for the content type:
def get_vocabularies(self, type = None):
vocabularies = {}
object = namedtuple("Vocabulary", "vid name desc help relations hierarchy multiple req tags weight")
if (type != None):
vocabs = VocabularyTypes.objects.filter(type=type)
for vocab in vocabs:
v = Vocabulary.objects.get(pk=vocab.vid)
vocabularies[v.vid] = object(vid = v.vid,
name = v.name,
desc = v.desc,
help = v.help,
relations = v.relations,
hierarchy = v.hierarchy,
multiple = v.multiple,
req = v.req,
tags = v.tags,
weight = v.weight
)
else:
vocabs = Vocabulary.objects.all()
for v in vocabs:
vocab = Vocabulary.objects.get(pk=v.vid)
vocabularies[v.vid] = object(vid = vocab.vid,
name = vocab.name,
desc = vocab.desc,
help = vocab.help,
relations = vocab.relations,
hierarchy = vocab.hierarchy,
multiple = vocab.multiple,
req = vocab.req,
tags = vocab.tags,
weight = vocab.weight
)
return vocabularies
So code I have tried so far is this:
def free_tags(self, type):
vocabs = Label.objects.get_vocabularies(type)
for vid in vocabs.keys():
output = forms.CharField(label=vocabs[vid].name,
required = vocabs[vid].req,
widget = forms.TextInput(attrs={'size' : 64})
)
return output
However, when using it in a view it prints out the internal python identifier (object intance at ... etc).
Another problem however, it that these field have no name i.e. they are all called 'output'.
So what I need to find out is how output a number of fields, when the number of fields is unknown.
This article helped me a lot to get started with dynamic forms.
The best way (I think) is this: You have function that creates your form class, e.g.
def make_form(instance):
fields = { 'a': forms.CharField(max_length=50),
'b': forms.CharField(widget=forms.Textarea) }
if some_condition:
fields['additional'] = forms.CharField()
return type('SomeForm', (forms.BaseForm,), { 'base_fields': fields })
You just define some fields in a dictionary, set certain fields only if conditions are fulfilled or not (instance
is some model instance that you want to test for conditions) and create a new class with Python's type()
function.
这篇关于动态django表单 - 变量字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!