Hello,

I have a form with three fields (1 Choicefield and 2 ModelChoicefields):  
The Choicefield displays names of objects by id's. However, in my db, I 
have multiple objects with the same name.  I am trying to filter these 
objects by "species" and "gender" (the two ModelChoiceFields).  I would 
like to display these objects as singular values, so they can be selected 
from the Choicefield without it displaying the same name more than once.  I 
am new to Django, so I "attempted" to write code to accomplish this in my 
form.py.  The following code displays multiple id's and singular names.  
However, I am having a lot of trouble trying to filter these items using 
functions in my views.py and JQuery in my template.  Basically, I can only 
get the names that have single Id's naturally associated to filter in the 
species and gender drop-downs, not the names with the multiple id's.  I 
realize the following code may be way off base, so I'm wondering if someone 
could enlighten me on how I could go about this dilemma.  Many thanks in 
advance.  By the way, I have written a Crispy Form's "Form Helper" Class 
which aided in display of my Tabs, so that is why my Tab Layout does not 
look complete.

# crispy forms 
class AnimalsList(forms.ModelForm):
    #namequery = 
Animal.objects.values_list('id','name').order_by('name','id')
    namelist = forms.ChoiceField(label='Animal Names', choices=[])     
    specieslist = forms.ChoiceField(label= 'by Species', choices=[], 
required=False)
    genderlist = forms.ChoiceField(label= 'by Gender', choices=[], 
required=False)
    
    class Meta:        
        model = Animal
        fields = ['namelist', 'specieslist', 'genderlist']
        
    def __init__(self, *args, **kwargs):
        #namequery = 
Animal.objects.values_list('id','name').order_by('name','id').annotate(the_count=Count('name'))
        # get count of animal name to determine duplicates
        count = 0
        did_list = list()        
        mydid_list = list()
        myd = myDict()
        # to return animal obj, count of name
        queryset = Animal.objects.values('name').annotate(count = 
Count('name'))
        for i in queryset: 
            animal_name = i["name"]                     
            if i["count"] <= 1:               
                animal = Animal.objects.get(name = animal_name)
                singular_id = str(animal.id)              
                myd.add(singular_id, animal_name)
            else:               
                dupe_list = Animal.objects.filter(name = animal_name)
                for dupe in dupe_list:
                    dupe_id = dupe.id                    
                    did_list.append(dupe_id)
                if len(did_list) > 1:
                        myd.add(tuple(did_list), animal_name)
           
        # set namequery to equal dictionary items - list of animal id and 
value        
        #namequery = tuple(dict.items())
        namequery = tuple(myd.items())      
        super().__init__(*args, **kwargs)       
        self.fields['namelist'].choices = (('', 'Select'),) + namequery
        self.fields['specieslist'].choices = (('', 'Select'),) + 
tuple(Animal.objects.none())
        self.fields['genderlist'].choices = (('', 'Select'),) + 
tuple(Animal.objects.none())
        self.helper = FormHelper()        
        self.helper.form_tag = False       
        self.helper.layout = Layout(
            Column(                                     
                   Div('namelist'), 
                   
Div('specieslist'),                                              
                   Div('genderlist')                    
                  ),
               )   

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/6c872186-a592-4696-b74c-d92e218a2a1cn%40googlegroups.com.

Reply via email to