django 外鍵創(chuàng)建注意事項說明
創(chuà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) # 添加外鍵的時候publish 可以不加引號;如果不加引號外鍵就要寫在主表上面,否則查找不到。添加引號則是按照映射關(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ǔ)充知識:Django重寫User外鍵重復(fù)問題
python Migrate 出現(xiàn)以下錯誤
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’
即可解決問題。
以上這篇django 外鍵創(chuàng)建注意事項說明就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python3實現(xiàn)往mysql中插入datetime類型的數(shù)據(jù)2. moment轉(zhuǎn)化時間戳出現(xiàn)Invalid Date的問題及解決3. python爬蟲實戰(zhàn)之制作屬于自己的一個IP代理模塊4. PHP如何打印跟蹤調(diào)試信息5. python如何實現(xiàn)word批量轉(zhuǎn)HTML6. Java8內(nèi)存模型PermGen Metaspace實例解析7. ASP中if語句、select 、while循環(huán)的使用方法8. msxml3.dll 錯誤 800c0019 系統(tǒng)錯誤:-2146697191解決方法9. 匹配模式 - XSL教程 - 410. 小技巧處理div內(nèi)容溢出
