Categories
Python Answers

How to iterating through two lists in Python Django templates?

To iterating through two lists in Python Django templates, we can zip the lists in our view and pass the zipped list into our template.

For instance, we write

mylist = zip(list1, list2)
context = {
            'mylist': mylist,
        }
return render(request, 'template.html', context)

to zip list1 and list2 into one with zip and assign it to mylist.

And then we call render with the context dict to pass the mylist value to the template.html template.

Then in template.html, we add

{% for item1, item2 in mylist %}

to loop through mylist with a for loop.

Categories
Python Answers

How to set Model Field Default Based Off Another Field in Same Model with Python Django?

To set Model Field Default Based Off Another Field in Same Model with Python Django, we can override the save method to set the default value.

For instance, we write

def save(self, *args, **kwargs):
    if not self.subject_init:
        self.subject_init = self.subject_initials()
    super(Subject, self).save(*args, **kwargs)

to add the save method to a model class.

In it, we set the default value of the subject_init field to the result returned by self.subject_initials().

And then we call the super class’ save method with the arguments from the parameters.

Categories
Python Answers

How to update an object from edit form in Python Django?

To update an object from edit form in Python Django, we can call save in our view.

For instance, we write

class MyForm(forms.ModelForm):
    class Meta:
        model = MyModel

in forms.py to create a form.

And then in views.py, we write

def my_view(request, id): 
    instance = get_object_or_404(MyModel, id=id)
    form = MyForm(request.POST or None, instance=instance)
    if form.is_valid():
        form.save()
        return redirect('next_view')
    return render(request, 'my_template.html', {'form': form}) 

to create the MyForm instance with request.POST and the MyModel object we want to by the id.

And we call form.save to save the form is the form‘s values are valid as returned by is_valid.

Otherwise, we render the form in the template.

Categories
Python Answers

How to group by and aggregate with Python Django?

Sometimes, we want to group by and aggregate with Python Django.

In this article, we’ll look at how to group by and aggregate with Python Django.

How to group by and aggregate with Python Django?

To group by and aggregate with Python Django, we call values and an aggergate function.

For instance, we write

Rating.objects.filter(attribute__in=attributes) 
    .values('location') 
    .annotate(score = Sum('score')) 
    .order_by('-score')

to call values with 'location' to group by location.

And then we call annotate with score set to the result of calling Sum with 'score' to sum up the grouped items’ score values.

Conclusion

To group by and aggregate with Python Django, we call values and an aggergate function.

Categories
Python Answers

How to set the selected value on a Python Django forms.ChoiceField

Sometimes, we want to set the selected value on a Python Django forms.ChoiceField.

In this article, we’ll look at how to set the selected value on a Python Django forms.ChoiceField.

How to set the selected value on a Python Django forms.ChoiceField

To set the selected value on a Python Django forms.ChoiceField, we set the initial argument when we’re calling the form constructor.

For instance, we write

form = MyForm(initial={'max_number': '3'})

to create the MyForm instance with the initial argument set to {'max_number': '3'} to set the initial value of the max_number field to 3.

Conclusion

To set the selected value on a Python Django forms.ChoiceField, we set the initial argument when we’re calling the form constructor.