Login

ContentType Form

Author:
nosrednakram
Posted:
September 22, 2009
Language:
Python
Version:
1.1
Tags:
form contenttype
Score:
0 (after 0 ratings)

When working with the ContentType model there are generally two issues.

  1. Models are listed in the table but not imported.
  2. Unicode only returns model not application so being able to select a a app/model is sometimes difficult when to applications have a model with the same name.

This snippet gets a listing of imported models and creates a drop down for selection. I also included a function that uses the returned from to get and save the correct ContentType within the primary model.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
forms.py
=========================================================================
def CTChoices():
    try:
        mods = models.get_models()
        mod_list = []
        for mod in mods:
            ok = '%s.%s' % (mod._meta.app_label, mod._meta.object_name)
            oc = '%s.%s' % (mod._meta.app_label.title(), mod._meta.object_name)
            rec = (ok,oc)
            mod_list.append(rec)
        mod_list.sort()
        return mod_list
    except:
        return []

class CTForm(forms.Form):
    ct = forms.ChoiceField(choices=CTChoices(),
                           label='Application and Model to upload into')
========================================================================
views.py
========================================================================
def CreateCVSUpload(request,
                   template_name = 'csvloader/Upload.html'):
    if request.method == 'POST':
        UploadForm = CsvUploadForm(request.POST, request.FILES,\
                                   prefix='upload')
        ModelForm = CTForm(request.POST, request.FILES,\
                             prefix='model')
        if UploadForm.is_valid() and ModelForm.is_valid():
            upload = UploadForm.save(commit=False)
            (app_label, model) = request.POST.get('model-ct','').split('.')
            if settings.DEBUG:
                print '%s  --  %s' % (app_label, model)
            ct = ContentType.objects.get(app_label=app_label,
                                         model = model.lower)
            upload.model = ct
            upload.save()
            return HttpResponseRedirect(reverse('csvloader_index'))
    else:
        UploadForm = CsvUploadForm(prefix='upload')
        ModelForm  = CTForm(prefix='model')
    return render_to_response(template_name,
                              { 'forms':  [UploadForm, ModelForm] },
                              context_instance=RequestContext(request) )

More like this

  1. Improved model select field for generic relationships by kratorius 6 years, 2 months ago
  2. Display values from a bound (submitted) form by masida 1 year, 5 months ago
  3. Command to dump data as a python script by willhardy 7 years, 2 months ago
  4. Get object/list or None by lokesh 5 years, 12 months ago
  5. Subdirectory and subcontext include template tag with examples by t_rybik 5 years, 5 months ago

Comments

Please login first before commenting.