- Django 2 Web Development Cookbook
- Jake Kronika Aidas Bendoraitis
- 84字
- 2025-02-25 18:24:43
Getting ready
Let's suppose that you have the Idea model, with a foreign key pointing to the Category model, as follows:
# demo_app/models.py from django.db import models from django.utils.translation import ugettext_lazy as _ class Category(models.Model): title = models.CharField(_("Title"), max_length=200) def __str__(self): return self.title class Idea(models.Model): title = model.CharField(
_("Title"),
max_length=200) category = models.ForeignKey(Category, verbose_name=_("Category"),
null=True,
blank=True,
on_delete=models.SET_NULL) def __str__(self): return self.title
The initial migration should be created and executed by using the following commands:
(myproject_env)$ python3 manage.py makemigrations demo_app (myproject_env)$ python3 manage.py migrate demo_app