Skip to main content

Checking the right boxes

 A student created a modelform but was struggling with one of the fields. He wanted it to render as checkboxes that would allow the user to select multiple choices. I was surprised that it was actually a little challenging to find any article online that showed one how exactly to do this. Perhaps I wasn't using the right key words in my search? I did figure it out fairly quickly in the end. 


models.py

class Cats(models.Model):

    fav_toys = models.CharField(max_length=100)


forms.py

TOYS = [

    ('Rubberband', 'Rubberband'),

    ('Ball', 'Ball'),

    ('Feather', 'Feather'),

]

class CatForm(ModelForm):

    class Meta:

        model = Cats

        fields = '__all__'

        widgets = {

            'fav_toys': forms.CheckboxSelectMultiple(choices=TOYS), 

}


And that did the trick! It's interesting that when the choices were defined in models.py rather than forms.py, it didn't work. I'm not entirely sure as to why that was. Perhaps one day, I will find out why.

Comments

Popular posts from this blog

Using Modals to render forms

A student wanted to create several tables with foreign keys linking them. He was placing the forms all over the place and that just didn't seem to make sense to me. I suggested that he link all the forms to one main registration page, and while he worked on it, I came up with my own solution that I feel rather proud of. Though I'm well aware it is a very simple solution.  views.py def register (request): # form for team formteam = TeamForm(request.POST or None ) if formteam.is_valid(): formteam.save() return redirect( 'registerForTeam' ) else : print (formteam.errors) formteam = TeamForm() # form for coach formcoaches = CoachesForm(request.POST or None ) if formcoaches.is_valid(): formcoaches.save() return redirect( 'registerForTeam' ) else : print (formcoaches.errors) formcoaches = CoachesForm() # form for player formplayer = PlayersForm(request.POST or None ...

Shortcuts

 I saw one of my colleagues do something magical. He automatically fixed the alignment of student code on Visual Studio with just 1 little keyboard shortcut. Ctrl-k, followed by D. Holding down Ctrl the entire time Sometimes shortcuts really do work and make your life better!   

Finding your roots

I tried working on the Algorithms I course on Coursera a while back and I had no idea what was going on so I never continued with it. I decided to give it another try now that I've read up a little on algorithms. It's still using a lot of my brain cells but I am slowly making my way through it.  Learning about Quick-unions in Java(from the course) Java seems almost identical to C#. I've forgotten most of what I've learnt about C# but, there's enough in this brain for me to read this. I had such a tough time understanding the root method. I wrote it out in my notebook and worked through it to figure out how it functions and I am amazed! That line is so simple yet complex. And it reminded me of a binary tree LeetCode challenge I was trying to work on with my colleagues some time ago. I had no idea what binary trees even were at that point. The challenge involved finding roots and tre...