ContentType Form

 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 3 years, 11 months ago
  2. list of all app_label, model of existing contentTypes by vijay.shanker 4 months, 1 week ago
  3. Arbitrary length formset by Rupe 3 years, 9 months ago
  4. Modeli18n by pavl 3 years ago
  5. Improved Pickled Object Field by taavi223 3 years, 10 months ago

Comments

(Forgotten your password?)