gpt4 book ai didi

python - 如何将 Django Form Class 变量传递给嵌套类 Meta?

转载 作者:太空宇宙 更新时间:2023-11-03 19:24:50 25 4
gpt4 key购买 nike

你好。我现在有一点烦恼比什么都重要。下面的代码完全可以正常工作,正如预期的那样。基本上,它是一个 ModelForm,它动态地将其模型基于 url 中接收的字符串,或者基于实例的类(如果提供)。

我的问题是是否可以通过传递 model_name 变量将其抽象到另一个模块 forms.py 中。我可以将 model_name 传递给表单 Class 没有问题,但我不知道如何将其传递给 Meta 之后。有什么简单的方法可以做到这一点吗?如果不是,这也可以,但它会让我的 View 代码堆更整洁。

@user_passes_test(lambda u: u.is_staff, login_url="%slogin/" % NINJA_ADMIN_URL_PREFIX)
def content_form(request, model_name=None, edit=False, call_name=''):
if edit:
content = Content.objects.get(call_name=call_name)
model_name = content.fields.__class__.__name__

class ContentForm(forms.ModelForm):
parent = ModelTextField(queryset=Content.objects.all(), widget=JQueryAutocomplete(
source_url='%sjson/call_names.json' % NINJA_ADMIN_URL_PREFIX, jquery_opts = {'minLength': 2},
override_label='item.fields.call_name', override_value='item.fields.call_name'),
required=False)

class Meta():
model = get_ninja_type(model_name)
widgets = {
'ninja_type': forms.widgets.HiddenInput(),
}

def clean_parent(self):
call_name = self.cleaned_data['parent']
if call_name:
try:
parent = Content.objects.get(call_name=call_name)
except Content.DoesNotExist:
raise forms.ValidationError("The call name '%s' doesn't exist. Choose another parent." % call_name)
return parent
else:
return None

if request.method == 'POST':
if edit:
form = ContentForm(request.POST, instance=content.fields)
else:
form = ContentForm(request.POST)
if form.is_valid():
content = form.save()
messages.success(request, "Your new content has been saved.")
return HttpResponseRedirect('%scontent/' % NINJA_ADMIN_URL_PREFIX)

else:
if edit:
form = ContentForm(instance=content.fields)
else:
form = ContentForm(initial={'ninja_type': model_name.lower(),
'author': request.user})
if edit:
page_title = 'Edit %s' % model_name
else:
page_title = 'Create New %s' % model_name

return render(request, 'ninja/admin/content_form.html', {
'form': form,
'ninja_type': model_name,
'page_title': page_title,
'edit': edit,
'meta_field_names': NINJA_META_FIELD_NAMES,
})

最佳答案

当我思考这个问题时,答案是非常明显的。返回类的函数可以正常工作。这是views.py 开头留下的内容。我认为这可能是最好的方法,或者至少是最简洁的方法。

@user_passes_test(lambda u: u.is_staff, login_url="%slogin/" % NINJA_ADMIN_URL_PREFIX)
def content_form(request, model_name=None, edit=False, call_name=''):
if edit:
content = Content.objects.get(call_name=call_name)
model_name = content.fields.__class__.__name__

ContentForm = get_content_form(model_name)

if request.method == 'POST':
if edit:
form = ContentForm(request.POST, instance=content.fields)
else:

这是 forms.py 条目。

def get_content_form(model_name):
class DynamicContentForm(forms.ModelForm):
parent = ModelTextField(queryset=Content.objects.all(), widget=JQueryAutocomplete(
source_url='%sjson/call_names.json' % NINJA_ADMIN_URL_PREFIX, jquery_opts = {'minLength': 2},
override_label='item.fields.call_name', override_value='item.fields.call_name'),
required=False)

class Meta():
model = get_ninja_type(model_name)
widgets = {
'ninja_type': forms.widgets.HiddenInput(),
}

def clean_parent(self):
call_name = self.cleaned_data['parent']
if call_name:
try:
parent = Content.objects.get(call_name=call_name)
except Content.DoesNotExist:
raise forms.ValidationError("The call name '%s' doesn't exist. Choose another parent." % call_name)
return parent
else:
return None
return DynamicContentForm

关于python - 如何将 Django Form Class 变量传递给嵌套类 Meta?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8549582/

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