django 外鍵創(chuàng)建注意事項(xiàng)說(shuō)明
創(chuàng)建表需要鏈接外鍵時(shí),需要注意的事項(xiàng)。
class Book(models.Model): name=models.CharField(max_length=20) price=models.IntegerField() pub_date=models.DateField() publish=models.ForeignKey('Publish',on_delete=models.CASCADE) # 添加外鍵的時(shí)候publish 可以不加引號(hào);如果不加引號(hào)外鍵就要寫(xiě)在主表上面,否則查找不到。添加引號(hào)則是按照映射關(guān)系查找,就不用考慮先后順序。 # authors=models.ManyToManyField('Author') def __str__(self): return self.nameclass Publish(models.Model): name=models.CharField(max_length=32) city=models.CharField(max_length=32) def __str__(self): return self.name
補(bǔ)充知識(shí):Django重寫(xiě)User外鍵重復(fù)問(wèn)題
python Migrate 出現(xiàn)以下錯(cuò)誤
auth.User.groups: (fields.E304) Reverse accessor for ’User.groups’ clashes with reverse accessor for ’User.groups’.
HINT: Add or change a related_name argument to the definition for ’User.groups’ or ’User.groups’.
auth.User.user_permissions: (fields.E304) Reverse accessor for ’User.user_permissions’ clashes with reverse accessor for ’User.user_permissions’.
在setting里添加
AUTH_USER_MODEL = ’users.UserProfile’
即可解決問(wèn)題。
以上這篇django 外鍵創(chuàng)建注意事項(xiàng)說(shuō)明就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. CSS自定義滾動(dòng)條樣式案例詳解2. 使用ProcessBuilder調(diào)用外部命令,并返回大量結(jié)果3. python b站視頻下載的五種版本4. python中if嵌套命令實(shí)例講解5. python中HTMLParser模塊知識(shí)點(diǎn)總結(jié)6. PHP與已存在的Java應(yīng)用程序集成7. 詳解Vue中Axios封裝API接口的思路及方法8. python 批量下載bilibili視頻的gui程序9. python鏈表類(lèi)中獲取元素實(shí)例方法10. 使用css實(shí)現(xiàn)全兼容tooltip提示框
