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

Django 解決阿里云部署同步數(shù)據(jù)庫(kù)報(bào)錯(cuò)的問(wèn)題

 更新時(shí)間:2020年05月14日 10:25:49   作者:請(qǐng)叫我算術(shù)嘉  
這篇文章主要介紹了Django 解決阿里云部署同步數(shù)據(jù)庫(kù)報(bào)錯(cuò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

寫在最前面:

在阿里云租了一臺(tái)服務(wù)器,搭建了一個(gè)博客,采用的是Ubuntu+Django+uwsgi+nginx+mysql的結(jié)構(gòu)。

運(yùn)行了一段時(shí)間后,我發(fā)現(xiàn)我忘記了django自帶后臺(tái)的密碼!

然后很常規(guī)的修改密碼的操作,就是無(wú)法登陸!

然后想再創(chuàng)建一個(gè)超級(jí)用戶,登上去看看什么情況,結(jié)果創(chuàng)建超級(jí)用戶又報(bào)錯(cuò)?

可是本地環(huán)境是ok的,然后同步數(shù)據(jù)庫(kù)出錯(cuò)。。。反正沒(méi)有對(duì)的。

然后同步數(shù)據(jù)庫(kù)報(bào)錯(cuò)如下:

手機(jī)端截的圖,查了一下報(bào)錯(cuò),應(yīng)該是setting.py的配置問(wèn)題,然后我把生產(chǎn)上的代碼拿下來(lái)看了下。

如下:

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = (
 os.path.join(BASE_DIR, 'static'),
 #os.path.join(os.path.dirname(__file__), '../static/').replace('\\', '/'),
)

這里要注意,STATIC_ROOT和STATICFILES_DIRS只要配置一個(gè)就可以!

如果非要同時(shí)配置

請(qǐng)將

STATIC_ROOT = os.path.join(BASE_DIR, 'static')

改為

STATIC_ROOT = os.path.join(BASE_DIR, '/static/')

然后同步數(shù)據(jù)庫(kù)

接下來(lái)創(chuàng)建超級(jí)用戶也沒(méi)有問(wèn)題了

登錄到admin后臺(tái)一看,原來(lái)的那個(gè)賬號(hào)權(quán)限被關(guān)了。。。怪不得怎么修改密碼都沒(méi)有用。

有空會(huì)詳細(xì)講講我在阿里云部署Django的過(guò)程。

補(bǔ)充知識(shí):django2.0 foreignKey提示on_delete

據(jù)說(shuō)在django2.0之前創(chuàng)建外鍵foreignKey的參數(shù)on_delete是有默認(rèn)值的,所以這個(gè)參數(shù)可以不用填,但在2.0之后on_delete沒(méi)有默認(rèn)值了,所以這個(gè)參數(shù)一定要傳,不然就報(bào)以下的錯(cuò):

TypeError: __init__() missing 1 required positional argument: on_delete

所以現(xiàn)在就來(lái)說(shuō)一下關(guān)于這個(gè)on_delete要傳的參數(shù)所代表的含義

on_delete=None, # 刪除關(guān)聯(lián)表中的數(shù)據(jù)時(shí),當(dāng)前表與其關(guān)聯(lián)的field的行為

on_delete=models.CASCADE, # 刪除關(guān)聯(lián)數(shù)據(jù),與之關(guān)聯(lián)也刪除

on_delete=models.DO_NOTHING, # 刪除關(guān)聯(lián)數(shù)據(jù),什么也不做

on_delete=models.PROTECT, # 刪除關(guān)聯(lián)數(shù)據(jù),引發(fā)錯(cuò)誤ProtectedError

# models.ForeignKey('關(guān)聯(lián)表', on_delete=models.SET_NULL, blank=True, null=True)

on_delete=models.SET_NULL, # 刪除關(guān)聯(lián)數(shù)據(jù),與之關(guān)聯(lián)的值設(shè)置為null(前提FK字段需要設(shè)置為可空,一對(duì)一同理)

# models.ForeignKey('關(guān)聯(lián)表', on_delete=models.SET_DEFAULT, default='默認(rèn)值')

on_delete=models.SET_DEFAULT, # 刪除關(guān)聯(lián)數(shù)據(jù),與之關(guān)聯(lián)的值設(shè)置為默認(rèn)值(前提FK字段需要設(shè)置默認(rèn)值,一對(duì)一同理)

on_delete=models.SET, # 刪除關(guān)聯(lián)數(shù)據(jù),

a. 與之關(guān)聯(lián)的值設(shè)置為指定值,設(shè)置:models.SET(值)

b. 與之關(guān)聯(lián)的值設(shè)置為可執(zhí)行對(duì)象的返回值,設(shè)置:models.SET(可執(zhí)行對(duì)象)

例,創(chuàng)建一對(duì)多外鍵

class UserType(models.Model):
 caption = models.CharField(max_length=32)
 
class UserInfo(models.Model):
 user = models.CharField(max_length=32)
 email = models.EmailField()
 user_type = models.ForeignKey(to="UserType",to_field="id",on_delete=models.CASCADE)

創(chuàng)建外鍵后,直接用models.xxxx.objects.create()創(chuàng)建數(shù)據(jù)時(shí)需要注意,外鍵這個(gè)值需要傳關(guān)聯(lián)表的對(duì)象,如下:

class UserType(models.Model):
 caption = models.CharField(max_length=32)
 
class UserInfo(models.Model):
 user = models.CharField(verbose_name='用戶', max_length=32)
 email = models.EmailField()
 user_type = models.ForeignKey(to="UserType",to_field="id",on_delete=models.CASCADE)
-----------上面是的是在models.py,下面的是在views.py-------------
def test(requset):
 ut = models.UserType.objects.filter(id=1).first()
 #print(ut)
 models.UserInfo.objects.create(user='小明',email='abc@163.com',user_type=ut)
 return HttpResponse('ok')

一對(duì)多的繼承代碼:

class ForeignKey(ForeignObject):
 def __init__(self, to, on_delete, related_name=None, related_query_name=None,
 limit_choices_to=None, parent_link=False, to_field=None,
 db_constraint=True, **kwargs):
  super().__init__(to, on_delete, from_fields=['self'], to_fields=[to_field], **kwargs)

創(chuàng)建一對(duì)一

OneToOneField(ForeignKey)
  to,   # 要進(jìn)行關(guān)聯(lián)的表名
  to_field=None    # 要關(guān)聯(lián)的表中的字段名稱
  on_delete=None,    # 當(dāng)刪除關(guān)聯(lián)表中的數(shù)據(jù)時(shí),當(dāng)前表與其關(guān)聯(lián)的行的行為
 
 ###### 對(duì)于一對(duì)一 ######
 # 1. 一對(duì)一其實(shí)就是 一對(duì)多 + 唯一索引
 # 2.當(dāng)兩個(gè)類之間有繼承關(guān)系時(shí),默認(rèn)會(huì)創(chuàng)建一個(gè)一對(duì)一字段
 # 如下會(huì)在A表中額外增加一個(gè)c_ptr_id列且唯一:
class C(models.Model):
nid = models.AutoField(primary_key=True)
part = models.CharField(max_length=12)
 
class A(C):
id = models.AutoField(primary_key=True)
code = models.CharField(max_length=1)

一對(duì)一的繼承代碼:

class OneToOneField(ForeignKey):
 def __init__(self, to, on_delete, to_field=None, **kwargs):
  kwargs['unique'] = True
  super().__init__(to, on_delete, to_field=to_field, **kwargs)

創(chuàng)建多對(duì)多

方式一:自定義關(guān)系表

class Host(models.Model):
 nid = models.AutoField(primary_key=True)
 hostname = models.CharField(max_length=32,db_index=True)
 ip = models.GenericIPAddressField(protocol="ipv4",db_index=True)
 port = models.IntegerField()
 b = models.ForeignKey(to="Business", to_field='id')
 # 10
class Application(models.Model):
 name = models.CharField(max_length=32)
 # 2
 
class HostToApp(models.Model):
 hobj = models.ForeignKey(to='Host',to_field='nid')
 aobj = models.ForeignKey(to='Application',to_field='id')
 
 
# HostToApp.objects.create(hobj_id=1,aobj_id=2)這里可以直接對(duì)第三張表直接操

方式二:自動(dòng)創(chuàng)建關(guān)系表

class Host(models.Model):
 nid = models.AutoField(primary_key=True)
 hostname = models.CharField(max_length=32,db_index=True)
 ip = models.GenericIPAddressField(protocol="ipv4",db_index=True)
 port = models.IntegerField()
 b = models.ForeignKey(to="Business", to_field='id')
 # 10
class Application(models.Model):
 name = models.CharField(max_length=32)
 r = models.ManyToManyField("Host") --------------> appname_application_r 表名

無(wú)法直接對(duì)第三張表進(jìn)行操作

只能間接操作————————————————————

obj = models.Application.objects.get(id=1)
obj.name
 
# 第三張表操作:HostToApp table   基于id=1的Application添加對(duì)應(yīng)關(guān)系
obj.r.add(1)增
obj.r.add(2)
obj.r.add(2,3,4)
obj.r.add(*[1,2,3,4])
 
obj.r.remove(1)   刪
obj.r.remove(2,4)
obj.r.remove(*[1,2,3])
 
obj.r.clear() 清除app_id =1 的列
 
obj.r.set([3,5,7])   改set將原來(lái)數(shù)據(jù)庫(kù)中的關(guān)系先全部刪除,在添加1-3,1-5,1-7
——————————————————————————
# 所有相關(guān)的主機(jī)對(duì)象“列表” QuerySet
obj.r.all()   obj.filter() obj.first()

前端取

  {%for app in app_list%}
  <tr>
 <td>{{app.name}}</td>
 <td>{{app.r.all}}</td>
   </tr>
  {%endfor%}

多對(duì)多的繼承代碼:

class ManyToManyField(RelatedField):
 def __init__(self, to, related_name=None, related_query_name=None,
 limit_choices_to=None, symmetrical=None, through=None,
 through_fields=None, db_constraint=True, db_table=None,
 swappable=True, **kwargs):
  super().__init__(**kwargs)

以上這篇Django 解決阿里云部署同步數(shù)據(jù)庫(kù)報(bào)錯(cuò)的問(wèn)題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論