一、django-admin的简单回顾:

admin: Django的后台数据管理的web版本

1、admin

  a:models.py

     - 创建表

  b:admin.py

    - 注册表    admin.site.register(models.UserInfo)

  c:urls.py

   - url(r'^admin/', admin.site.urls),

  PS:

    1、动态生成url

    2、注册和生成url使用的都是admin.site

二、django-admin的用法

当我们创建登录admin的时候,里面会有增删改查,不仅仅是这些功能,我们可以通过以下的方式设置一些样式

1、admin路由规则:

/admin/app01/role/           查看角色列表
/admin/app01/role/add/       添加角色
/admin/app01/role/2/change/  编辑
/admin/app01/role/2/delete/  删除
            
            
/admin/app01/userinfo/           /admin/应用名/表名
/admin/app01/userinfo/add/       /admin/应用名/表名/功能名

/admin/app01/userinfo/1/change/ /admin/app01/userinfo/1/delete/

 

2.自定义admin

创建models

from django.db import models

# Create your models here.


from django.db import models


# Create your models here.


class Author(models.Model):
    nid = models.AutoField(primary_key=True)
    name = models.CharField(max_length=32)
    age = models.IntegerField()

    # 与AuthorDetail建立一对一的关系
    authorDetail = models.OneToOneField(to="AuthorDetail", on_delete=models.CASCADE)

    def __str__(self):
        return self.name


class AuthorDetail(models.Model):
    nid = models.AutoField(primary_key=True)
    birthday = models.DateField()
    telephone = models.BigIntegerField()
    addr = models.CharField(max_length=64)


class Publish(models.Model):
    nid = models.AutoField(primary_key=True)
    name = models.CharField(max_length=32)
    city = models.CharField(max_length=32)
    email = models.EmailField()

    def __str__(self):
        return self.name


class Book(models.Model):
    nid = models.AutoField(primary_key=True)
    title = models.CharField(max_length=32)
    publishDate = models.DateField()
    price = models.DecimalField(max_digits=5, decimal_places=2)

    # 与Publish建立一对多的关系,外键字段建立在多的一方
    publish = models.ForeignKey(to="Publish", to_field="nid", on_delete=models.CASCADE)
    # 与Author表建立多对多的关系,ManyToManyField可以建在两个模型中的任意一个,自动创建第三张表
    authors = models.ManyToManyField(to='Author', )

    def __str__(self):
        return self.title
models.py

相关文章:

猜你喜欢
  • 2021-10-25
  • 2022-02-23
  • 2021-10-07
  • 2022-12-23
  • 2021-12-18
  • 2021-06-09
相关资源
相似解决方案