Categories
Python Answers

How to get query string parameters with Python Django?

Sometimes, we want to get query string parameters with Python Django.

In this article, we’ll look at how to get query string parameters with Python Django.

How to get query string parameters with Python Django?

To get query string parameters with Python Django, we can use the request.GET property.

For instance, we write

def get_item(request):
    id = int(request.GET.get('id'))
    type = request.GET.get('type', 'default')

in the get_item view function to get the id query parameter with

id = int(request.GET.get('id'))

And we get the type query parameter with

type = request.GET.get('type', 'default')

The type query parameter has default value 'default' if it doesn’t exist.

Conclusion

To get query string parameters with Python Django, we can use the request.GET property.

Categories
Python Answers

How to add composite primary key in Python Django?

Sometimes, we want to add composite primary key in Python Django.

In this article, we’ll look at how to add composite primary key in Python Django.

How to add composite primary key in Python Django?

To add composite primary key in Python Django, we can set the unique_together field in the Meta class in the model class.

For instance, we write

class MyTable(models.Model):
    class Meta:
        unique_together = (('key1', 'key2'),)

    key1 = models.IntegerField(primary_key=True)
    key2 = models.IntegerField()

to set unique_together to (('key1', 'key2'),) to make key1 and key2 part of the composite primary key.

Conclusion

To add composite primary key in Python Django, we can set the unique_together field in the Meta class in the model class.

Categories
Python Answers

How to show datepicker calendar on datefield with Python Django?

Sometimes, we want to show datepicker calendar on datefield with Python Django.

In this article, we’ll look at how to show datepicker calendar on datefield with Python Django.

How to show datepicker calendar on datefield with Python Django?

To show datepicker calendar on datefield with Python Django, we can add a date input into the form template.

For instance, we write

from django import forms
class HolidayTimeForm(forms.Form):
    holiday_date = forms.DateField(widget=forms.TextInput(attrs=
                                {
                                    'class':'datepicker'
                                }))

to add a date field into our form.

And then in our form’s template, we add

<script>
  $(function () {
    $(".datepicker").datepicker({
      changeMonth: true,
      changeYear: true,
      yearRange: "1900:2022",
      // ...
    });
  });
</script>

to call jQuery UI’s datepicker method to turn the element with class datepicker into a date picker.

Conclusion

To show datepicker calendar on datefield with Python Django, we can add a date input into the form template.

Categories
Python Answers

How to find the union of two Python Django querysets?

Sometimes, we want to find the union of two Python Django querysets.

In this article, we’ll look at how to find the union of two Python Django querysets.

How to find the union of two Python Django querysets?

To find the union of two Python Django querysets, we use the | operator.

For instance, we write

records = query1 | query2

If we want distinct values, we write

records = (query1 | query2).distinct()

Conclusion

To find the union of two Python Django querysets, we use the | operator.

Categories
Python Answers

How to add a Foreign Key Field to a ModelForm in Python Django?

Sometimes, we want to add a Foreign Key Field to a ModelForm in Python Django.

In this article, we’ll look at how to add a Foreign Key Field to a ModelForm in Python Django.

How to add a Foreign Key Field to a ModelForm in Python Django?

To add a Foreign Key Field to a ModelForm in Python Django, we can add the field to the self.fields dict.

For instance, we write

class DocumentForm(forms.ModelForm):
    class Meta:
        model = Document

    def __init__(self, *args, **kwargs):
        user = kwargs.pop('user','')
        super(DocumentForm, self).__init__(*args, **kwargs)
        self.fields['user_defined_code']=forms.ModelChoiceField(queryset=UserDefinedCode.objects.filter(owner=user))

to add the user_defined_code field by writing

self.fields['user_defined_code']=forms.ModelChoiceField(queryset=UserDefinedCode.objects.filter(owner=user))

We get the choices for the field by setting the queryset argument to UserDefinedCode.objects.filter(owner=user).

Conclusion

To add a Foreign Key Field to a ModelForm in Python Django, we can add the field to the self.fields dict.