django: models

Error: No module named 'mysite.books'

Solution: delete mysite in settings.py, directly 'books',

错误: __init__() missing 1 required positional argument: 'on_delete'

解决:publisher = models.ForeignKey(Publisher,on_delete=models.CASCADE)

Added in parentheses, on_delete=models.CASCADE

Problem: python manage.py validate reports an error

Solution: python manage.py check instead


Used to detect database changes and generate database migration files:

python manage.py makemigrations books

To migrate the database:

python manage.py migrated

Used to convert database migration files into database languages

python manage.py sqlmigrate books 0001



In Django, if you are using Python3, you can only use the __str__ method, and if you are using Python2, you can use the __unicode__ method. Because it is safer.



The filter() function in publisher.objects.filter() returns a recordset

get() returns a single object, multiple or non-existent will be an error, use try--except--else


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325771651&siteId=291194637