Login

ContentType Form

Author:
nosrednakram
Posted:
September 22, 2009
Language:
Python
Version:
1.1
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. Browser-native date input field by kytta 1 month, 1 week ago
  2. Generate and render HTML Table by LLyaudet 1 month, 2 weeks ago
  3. My firs Snippets by GutemaG 1 month, 3 weeks ago
  4. FileField having auto upload_to path by junaidmgithub 2 months, 4 weeks ago
  5. LazyPrimaryKeyRelatedField by LLyaudet 3 months, 1 week ago

Comments

Please login first before commenting.