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. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.