Categories
Python Answers

How to change Python Django settings at runtime?

Sometimes, we want to change Python Django settings at runtime.

In this article, we’ll look at how to change Python Django settings at runtime.

How to change Python Django settings at runtime?

To change Python Django settings at runtime, we can set the conf.settings dictionary.

For instance, we write

import django.conf as conf

conf.settings.DATABASES['default']['NAME'] = 'abc'

to set the conf.settings.DATABASES['default']['NAME'] field to a value in the entry point of our app.

Conclusion

To change Python Django settings at runtime, we can set the conf.settings dictionary.

Categories
Python Answers

How to do a left join with the Python Django ORM?

Sometimes, we want to do a left join with the Python Django ORM.

In this article, we’ll look at how to do a left join with the Python Django ORM.

How to do a left join with the Python Django ORM?

To do a left join with the Python Django ORM, we can call filter with the related table field’s is_null argument set to True.

For instance, we write

Department.objects.filter(departmentvolunteer__department__isnull=True)

to call filter with the departmentvolunteer__department__isnull set to True to do a left join on the departmentvolunteer table by making the foreign key optional.

Conclusion

To do a left join with the Python Django ORM, we can call filter with the related table field’s is_null argument set to True.

Categories
Python Answers

How to store a dictionary on a Python Django Model?

Sometimes, we want to store a dictionary on a Python Django Model.

In this article, we’ll look at how to store a dictionary on a Python Django Model.

How to store a dictionary on a Python Django Model?

To store a dictionary on a Python Django Model, we can create a JSONField with the jsonfield package.

To install it, we run

pip install jsonfield

to install it.

Then we write

from django.db import models
from jsonfield import JSONField

class MyModel(models.Model):
    json = JSONField()

to create the json JSONField in MyModel to create the JSON field.

Conclusion

To store a dictionary on a Python Django Model, we can create a JSONField with the jsonfield package.

Categories
Python Answers

How to fix CSV new-line character seen in unquoted field error in Python?

Sometimes, we want to fix CSV new-line character seen in unquoted field error in Python.

In this article, we’ll look at how to fix CSV new-line character seen in unquoted field error in Python.

How to fix CSV new-line character seen in unquoted field error in Python?

To fix CSV new-line character seen in unquoted field error in Python, we can call csv.reader with the dialect argument.

For instance, we write

file_read = csv.reader(file, dialect=csv.excel_tab)

to call csv.reader with the CSV file returned from open.

And the dialect argument is set to csv.excel_tab to read it in as a Excel tab delimited CSV.

Conclusion

To fix CSV new-line character seen in unquoted field error in Python, we can call csv.reader with the dialect argument.

Categories
Python Answers

How to override admin CSS in Python Django?

Sometimes, we want to override admin CSS in Python Django.

In this article, we’ll look at how to override admin CSS in Python Django.

How to override admin CSS in Python Django?

To override admin CSS in Python Django, we can set the css file of the Media class in the model admin class.

For instance, we write

class MyModelAdmin(admin.ModelAdmin):
    class Media:
        js = ('js/admin/my_own_admin.js',)    
        css = {
             'all': ('css/admin/my_own_admin.css',)
        }

in admin.py to add the Media class in the MyModelAdmin class.

In it, we set the css field to a dict with the all key set to the CSS file path.

Conclusion

To override admin CSS in Python Django, we can set the css file of the Media class in the model admin class.