Categories
Python Answers

How to fix OperationalError No Such Table with Python Django?

Sometimes, we want to fix OperationalError No Such Table with Python Django.

In this article, we’ll look at how to fix OperationalError No Such Table with Python Django.

How to fix OperationalError No Such Table with Python Django?

To fix OperationalError No Such Table with Python Django, we run the migrations to add the missing table.

We run

python manage.py migrate --run-syncdb 

to create the table without migrations with --run-syncdb.

We can also make and run migrations with

python manage.py makemigrations app
python manage.py migrate app

Conclusion

To fix OperationalError No Such Table with Python Django, we run the migrations to add the missing table.

Categories
Python Answers

How to show the verbose version of a choice with Python Django templates?

Sometimes, we want to show the verbose version of a choice with Python Django templates.

In this article, we’ll look at how to show the verbose version of a choice with Python Django templates.

How to show the verbose version of a choice with Python Django templates?

To show the verbose version of a choice with Python Django templates, we can create a model class with our own method to return the verbose version of the choice.

For instance, we write

from django.db import models

class Scoop(models.Model):
    FLAVOR_CHOICES = [
        ('c', 'Chocolate'),
        ('v', 'Vanilla'),
    ]

    flavor = models.CharField(choices=FLAVOR_CHOICES)

    def flavor_verbose(self):
        return dict(Scoop.FLAVOR_CHOCIES)[self.flavor]

to create the flavor_verbose method.

In it, we convert FLAVOR_CHOICES to a dict with dict(Scoop.FLAVOR_CHOCIES).

And then we get the verbose version of the choice with self.flavor.

Then in our template, we write

{{ scoop.flavor_verbose }}

to show the choice.

Conclusion

To show the verbose version of a choice with Python Django templates, we can create a model class with our own method to return the verbose version of the choice.

Categories
Python Answers

How to add a ChoiceField with Django Rest Framework?

Sometimes, we want to add a ChoiceField with Django Rest Framework.

In this article, we’ll look at how to add a ChoiceField with Django Rest Framework.

How to add a ChoiceField with Django Rest Framework?

To add a ChoiceField with Django Rest Framework, we can set the source argument of the field.

For instance, we write

class User(AbstractUser):
    GENDER_CHOICES = (
        ('M', 'Male'),
        ('F', 'Female'),
    )

    gender = models.CharField(max_length=1, choices=GENDER_CHOICES)

to create a CharField in the User model.

Then we write

class UserSerializer(serializers.ModelSerializer):
    gender = serializers.CharField(source='get_gender_display')

    class Meta:
        model = User

to create the gender field in the UserSerializer to set the source argument to the 'get_gender_display' method.

get_gender_display is a method that’s automatically included with the User model.

Conclusion

To add a ChoiceField with Django Rest Framework, we can set the source argument of the field.

Categories
Python Answers

How to add Python Django unit tests without a database?

Sometimes, we want to add Python Django unit tests without a database.

In this article, we’ll look at how to add Python Django unit tests without a database.

How to add Python Django unit tests without a database?

To add Python Django unit tests without a database, we can create our own test runner class.

For instance, we write

from django.test.runner import DiscoverRunner

class NoDbTestRunner(DiscoverRunner):
  def setup_databases(self, **kwargs):
    pass

  def teardown_databases(self, old_config, **kwargs):
    pass

to add the NoDbTestRunner that overrides the code to set up the test database in setup_databases.

And in teardown_databases we override the code to clean the database after tests are run.

Then we create a settings file that makes Django use the test runner that we just created with

from mysite.settings import *

TEST_RUNNER = 'mysite.scripts.testrunner.NoDbTestRunner'

We put that in the no_db_settings.py file.

Then when we run our test, we run

python manage.py test myapp --settings='no_db_settings'

to run our tests with the no_db_settings.py file as the settings file.

Conclusion

To add Python Django unit tests without a database, we can create our own test runner class.

Categories
Python Answers

How to use the built in password reset/change views with my own templates with Python Django?

Sometimes, we want to use the built in password reset/change views with my own templates with Python Django.

In this article, we’ll look at how to use the built in password reset/change views with my own templates with Python Django.

How to use the built in password reset/change views with my own templates with Python Django?

To use the built in password reset/change views with my own templates with Python Django, we can create our own view that returns the response returned by the password_reset function.

For instance, we write

from django.contrib.auth.views import password_reset

def my_password_reset(request, template_name='path/to/my/template'):
    return password_reset(request, template_name)

to create the my_password_reset view function.

In it, we call password_reset(request, template_name) to return the response that renders the password reset form template.

Conclusion

To use the built in password reset/change views with my own templates with Python Django, we can create our own view that returns the response returned by the password_reset function.