亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

django修改models重建數(shù)據(jù)庫的操作

 更新時(shí)間:2020年03月31日 10:25:07   作者:yue492008824  
這篇文章主要介紹了django修改models重建數(shù)據(jù)庫的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

第二次修改models.py以后再次

python manage.py makemigrations

提示如下

You are trying to add a non-nullable field 'address' to xc_users without a default; we can't do that (the database needs something to populate existing rows). Please select a fix: 1) Provide a one-off default now (will be set on all existing rows) 2) Qu

于是刪除數(shù)據(jù)庫表,繼續(xù)報(bào)相同的錯(cuò)誤

makemigration需要指定名稱,然后migrate才能識(shí)別兩次<span style="font-family: Arial, Helvetica, sans-serif;">makemigrations的不同</span>

python manage.py makemigrations --name sqlname

然后再執(zhí)行:

python manage.py migrate

成功解決問題!

補(bǔ)充知識(shí):django rest framework如何一次性序列化同一個(gè)數(shù)據(jù)庫下多個(gè)model表數(shù)據(jù)在一個(gè)json頁面下

在django項(xiàng)目中,首先我們要安裝django-rest-mutiple-models

pip install django-rest-multiple-models

然后在setting配置中配置一下APP

INSTALLED_APPS = (
 ....
 'drf_multiple_model',
)


在我們的views視圖函數(shù)下引入

from drf_multiple_model.views import ObjectMultipleModelAPIView

如下兩個(gè)模型

class Play(models.Model):
 genre = models.CharField(max_length=100)
 title = models.CharField(max_length=200)
 pages = models.IntegerField()
 
class Poem(models.Model):
 title = models.CharField(max_length=200)
 style = models.CharField(max_length=100)
 lines = models.IntegerField()
 stanzas = models.IntegerField()

序列化如下

class PlaySerializer(serializers.ModelSerializer):
 class Meta:
 model = Play
 fields = ('genre','title','pages')
 
class PoemSerializer(serializers.ModelSerializer):
 class Meta:
 model = Poem
 fields = ('title','stanzas')

views中我們可以這么寫,繼承一個(gè)ObjectMutipleModelAPIView

from drf_multiple_model.views import ObjectMultipleModelAPIView
 
class TextAPIView(ObjectMultipleModelAPIView):
 querylist = [
 {'queryset': Play.objects.all(), 'serializer_class': PlaySerializer},
 {'queryset': Poem.objects.filter(style='Sonnet'), 'serializer_class': PoemSerializer},
 ....
 ]

如果我們使用的url是router,那這里我們就應(yīng)該繼承ObjectMutipleModelAPIViewset

具體的引入

from drf_multiple_model.viewsets import ObjectMultipleModelAPIViewSet

DRF頁面的顯示效果如下

{
 "Play" : [
 {"genre": "Comedy", "title": "A Midsummer Night"s Dream", "pages": 350},
 {"genre": "Tragedy", "title": "Romeo and Juliet", "pages": 300},
 ....
 ],
 "Poem" : [
 {"title": "Shall I compare thee to a summer"s day", "stanzas": 1},
 {"title": "As a decrepit father takes delight", "stanzas": 1},
 ....
 ],
}

效果就是能在一個(gè)json頁面下序列化出多個(gè)不同的model數(shù)據(jù)

我們還可以對(duì)每個(gè)表的數(shù)據(jù)進(jìn)行分頁

from drf_multiple_model.pagination import MultipleModelLimitOffsetPagination

views下可以這么寫

class LimitPagination(MultipleModelLimitOffsetPagination):
default_limit = 10

意思是每個(gè)model默認(rèn)顯示前十條數(shù)據(jù)

如果前端需要,url上可以加上limit=5,自定義默認(rèn)顯示前五條數(shù)據(jù)

更多詳細(xì)內(nèi)容,github搜索DjangoRestMultipleModels

以上這篇django修改models重建數(shù)據(jù)庫的操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python中的列表?xiàng)l件求和方法

    Python中的列表?xiàng)l件求和方法

    這篇文章主要介紹了Python中的列表?xiàng)l件求和方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • Python pandas DataFrame操作的實(shí)現(xiàn)代碼

    Python pandas DataFrame操作的實(shí)現(xiàn)代碼

    這篇文章主要介紹了Python pandas DataFrame操作的實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-06-06
  • 基于Python實(shí)現(xiàn)人臉識(shí)別和焦點(diǎn)人物檢測(cè)功能

    基于Python實(shí)現(xiàn)人臉識(shí)別和焦點(diǎn)人物檢測(cè)功能

    基于dlib庫的模型,實(shí)現(xiàn)人臉識(shí)別和焦點(diǎn)人物的檢測(cè)。最后呈現(xiàn)的效果為焦點(diǎn)人物的識(shí)別框顏色與其他人物框不一樣。對(duì)Python人臉識(shí)別和焦點(diǎn)人物檢測(cè)設(shè)計(jì)過程感興趣的朋友一起看看吧
    2021-10-10
  • Python Pygame實(shí)現(xiàn)兔子獵人守護(hù)城堡游戲

    Python Pygame實(shí)現(xiàn)兔子獵人守護(hù)城堡游戲

    這篇文章主要介紹了用python來制作的一個(gè)守護(hù)類小游戲兔子獵人守護(hù)城堡,文中的示例代碼介紹得很詳細(xì),感興趣的小伙伴快來跟隨小編一起學(xué)習(xí)學(xué)習(xí)吧
    2021-12-12
  • Python實(shí)現(xiàn)的從右到左字符串替換方法示例

    Python實(shí)現(xiàn)的從右到左字符串替換方法示例

    這篇文章主要介紹了Python實(shí)現(xiàn)的從右到左字符串替換方法,涉及Python字符串遍歷、運(yùn)算、判斷、替換等相關(guān)操作技巧,需要的朋友可以參考下
    2018-07-07
  • Python爬蟲利用多線程爬取 LOL 高清壁紙

    Python爬蟲利用多線程爬取 LOL 高清壁紙

    這篇文章主要介紹了Python爬蟲利用多線程爬取 LOL 高清壁紙,通過網(wǎng)站爬取每一個(gè)英雄的所有皮膚圖片,全部下載下來并保存到本地,下文爬取過程感興趣的朋友可以參考一下
    2022-06-06
  • Python實(shí)現(xiàn)按逗號(hào)分隔列表的方法

    Python實(shí)現(xiàn)按逗號(hào)分隔列表的方法

    今天小編就為大家分享一篇Python實(shí)現(xiàn)按逗號(hào)分隔列表的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • python實(shí)現(xiàn)斷點(diǎn)調(diào)試的方法

    python實(shí)現(xiàn)斷點(diǎn)調(diào)試的方法

    本文主要介紹了python實(shí)現(xiàn)斷點(diǎn)調(diào)試的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • Python利用watchdog模塊監(jiān)控文件變化

    Python利用watchdog模塊監(jiān)控文件變化

    這篇文章主要為大家介紹一個(gè)Python中的模塊:watchdog模塊,它可以實(shí)現(xiàn)監(jiān)控文件的變化。文中通過示例詳細(xì)介紹了watchdog模塊的使用,需要的可以參考一下
    2022-06-06
  • Python入門必須知道的11個(gè)知識(shí)點(diǎn)

    Python入門必須知道的11個(gè)知識(shí)點(diǎn)

    這篇文章主要為大家詳細(xì)介紹了Python入門必須知道的11個(gè)知識(shí)點(diǎn),幫助更好地了解python,感興趣的小伙伴們可以參考一下
    2018-03-03

最新評(píng)論