Django xadmin安裝及model設(shè)計(jì)

用戶mdoel設(shè)計(jì)

為了方便該model繼承與系統(tǒng)的 from django.contrib.auth.models import AbstractUser
在users app中的 models.py中

class UserProfile(AbstractUser):
"""
用戶
"""
name = models.CharField(max_length=30, null=True, blank=True, verbose_name="姓名")
birthday = models.DateField(null=True, blank=True, verbose_name="出生年月")
gender = models.CharField(max_length=6, choices=(("manle", u"男"), ("female", u"女")), verbose_name="性別")
mobile = models.CharField(null=True, blank=True, max_length=11, verbose_name="電話")
email = models.EmailField(max_length=100, null=True, blank=True, verbose_name="郵箱")

#配置該屬性為了在后臺(tái) UserProfile顯示為用戶字樣
class Meta:
    verbose_name = "用戶"
    verbose_name_plural = "用戶"

def __str__(self):
    return self.username;

配置
在setting.py中,添加

#配置了之后,表明為系統(tǒng)用戶
AUTH_USER_MODEL = 'users.UserProfile'

生成表操作

#相當(dāng)于生產(chǎn)sql語(yǔ)句的操作
migrate
#執(zhí)行sql語(yǔ)句
makemigration

創(chuàng)建管理員用戶

創(chuàng)建之前需要先編寫(xiě)model
運(yùn)行manage.py

#創(chuàng)建管理用戶
createsupperuser  #username root ,email root@163.com  password root1234

之后查看數(shù)據(jù)庫(kù),可以發(fā)現(xiàn)生成了相關(guān)的表
運(yùn)行項(xiàng)目: http://127.0.0.1:8000/admin 輸入帳號(hào)和密碼可以查看 這是django自帶的admi

源碼安裝xadmin

下載xadmin的源碼,復(fù)制到 extra_apps 文件中
配置:
xadmin依賴了一些三方包,在運(yùn)行的時(shí)候會(huì)出錯(cuò),安裝三方包即可(可以使用pip安裝xadmin,會(huì)安裝依賴包,之后卸載xadmin,復(fù)制源碼安裝)

INSTALLED_APPS =[
    ...,
    'xadmin',
    'crispy_forms',#xadmin依賴的東西
]

在users app 下新建 adminx.py文件
xadmin的全局配置:

    import xadmin
from xadmin import views
    class BaseSetting(object):
     enable_themes = True
     use_bootswatch = True

#xadmin全局配置,在xadmin管理界面顯示的標(biāo)題等信息
class GlobalSettings(object):
    site_title = "LOVE YOU 后臺(tái)"
    site_footer = "loveyou"
    # menu_style = "accordion"
#注冊(cè)信息
xadmin.site.register(views.BaseAdminView, BaseSetting)
xadmin.site.register(views.CommAdminView, GlobalSettings)

數(shù)據(jù)更新的操作,進(jìn)行migrage操作

migrate
makemigrations

之后訪問(wèn)xadmin查看效果

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容