SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Sep 2013
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Python + Django: Detect ModelChoiceField type in views.py ?

    I'm trying to iterate through a form's fields and check the type of field each one is. I want to implement special handling for ModelChoiceField. So this is basically what I'm using:



    models.py:
    Code Python:
    from django.db import models
     
    class MyInfo(models.Model):
        TypeID = models.IntegerField()
        SomeTextInfo = models.TextField()
        OtherTextInfo = models.TextField()
        BooleanInfo = models.BooleanField()
     
    class MyTypes(models.Model):
        MyTypeName = models.CharField(max_length = 100)
        def __unicode__(self):
            return self.MyTypeName

    forms.py:
    Code Python:
    from django import forms
    from MyProject.models import MyInfo
    from MyProject.models import MyTypes
     
    class MyForm(forms.Form):
        ID = forms.CharField(widget = forms.HiddenInput())
        TypeID = forms.ModelChoiceField(queryset=MyTypes.objects.all(),empty_label="(Unknown)")
        SomeTextInfo = forms.CharField()
        OtherTextInfo = forms.CharField()
        BooleanInfo = forms.RadioSelect()


    views.py:
    Code Python:
    from django.shortcuts import render_to_response
    from django.http import HttpResponseRedirect
    from django.contrib import auth
    from django.core.context_processors import csrf
    from django.template import RequestContext
    from django.contrib.auth.models import User
    from django import forms
    from MyProject.models import MyInfo	
    from MyProject.models import MyTypes
     
    def whatever(request):
        if request.method == "POST":
            my_form = MyForm(request.POST)
            if my_form.is_valid():
                q = MyInfo()
                for each in my_form:
                    #!!!!!!-----THIS RIGHT HERE IS NOT WORKING NO MATTER WHAT I TRY----!!!!!!
                    #if isinstance(each, forms.ModelChoiceField)
                    if type(each) is forms.ModelChoiceField:
                        #import sys
                        #sys.exit("Error message")
                        Value_Needed = my_form.cleaned_data[each.name].pk
                        setattr(q, each.name, Value_Needed)
                    else:
                        Value_Needed = my_form.cleaned_data[each.name]
                        setattr(q, each.name, Value_Needed)
                q.save()
        else:
            #just render the form because it hasn't been submitted yet......

    Unfortunately this not working. I know this because if I try un-commenting the "sys.exit" command in the "If" section where I'm doing the respective handling, it never gets hit. So its always executing whats in the "else" section. And whats more, when it gets there, I keep getting an error message that says "int() argument must be a string or a number, not 'MyTypes'.

    I'm stumped and have tried every variance I can think of to detect all ModelChoiceField types, but just can't seem to figure this out. Anyone have any suggestions?

    (And one more thing: please don't tell me I should be using "ModelMultupleChoiceField" because thats not what I'm using)

  2. #2
    SitePoint Member
    Join Date
    Sep 2013
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    this is the fix

    Ok I got some help and figured this out form another outside source. Thought I'd share the solution just in case someone else needs help with something similar at a later date. The change was to the "views.py" to file to use this "if" statement:

    Code Python:
    if type(each.field) is forms.ModelChoiceField:

    So much trouble for something so small and simple. Just had to add that ".field" part. Couldn't find a forum post to save my soul by anyone else doing anything similar. Hope this helps someone else avoid the same headache..


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •