Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The answer was pretty obvious when I thought about it. A function returning a class works fine. This is what's left at the start of views.py. I'm thinking this is likely the best way to do this, or at least the most concise.</p> <pre><code>@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: </code></pre> <p>This is the forms.py entry.</p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload