admin简单使用:
1.urls.py
![](https://images2015.cnblogs.com/blog/996586/201703/996586-20170302151529579-879293861.png)
2.settings.py
![](https://images2015.cnblogs.com/blog/996586/201703/996586-20170302151533610-1685864745.png)
3.models.py
from django.db import models
classUserInfo(models.Model):
uid = models.AutoField(primary_key=True)
user_name = models.CharField(max_length=30)
email = models.EmailField()
memo = models.TextField()
img = models.ImageField()
4.admin.py
![](https://images2015.cnblogs.com/blog/996586/201703/996586-20170302151536063-1994156613.png)
5.终端执行:
![](https://images2015.cnblogs.com/blog/996586/201703/996586-20170302151537626-1249382430.png)
执行上述2个命令,在数据库建表。
![](https://images2015.cnblogs.com/blog/996586/201703/996586-20170302151540329-373515889.png)
6.创建超级用户
![](https://images2015.cnblogs.com/blog/996586/201703/996586-20170302151544485-690699327.png)
登录之后:
![](https://images2015.cnblogs.com/blog/996586/201703/996586-20170302151546860-419400785.png)
可以看到对应的字段显示:
![](https://images2015.cnblogs.com/blog/996586/201703/996586-20170302151549173-2144287893.png)
由于在创建表的时候指定的emailfield,所以admin会自动检测输入是否符合邮箱格式。
![](https://images2015.cnblogs.com/blog/996586/201703/996586-20170302151551720-448345569.png)
至此,简单的admin应用就完成了。
如果我们想要在添加表,如何操作呢?
1.在models.py 中添加类
![](https://images2015.cnblogs.com/blog/996586/201703/996586-20170302151553329-246354825.png)
2.同样,我们可以在userinfo中添加外键
![](https://images2015.cnblogs.com/blog/996586/201703/996586-20170302151554970-1280265880.png)
其中的null=True表示数据库表中的该字段可以为空,blank=True表示admin中是允许用户输入为空
3.执行建表命令
![](https://images2015.cnblogs.com/blog/996586/201703/996586-20170302151556829-678771098.png)
可以看到新加的表usertype:
![](https://images2015.cnblogs.com/blog/996586/201703/996586-20170302151601376-543678468.png)
和原来的userinfo表:
![](https://images2015.cnblogs.com/blog/996586/201703/996586-20170302151604907-1624515441.png)
此时,我们可以通过admin添加用户类型了
![](https://images2015.cnblogs.com/blog/996586/201703/996586-20170302151607188-615742270.png)
添加之后可以在数据库查看到:
![](https://images2015.cnblogs.com/blog/996586/201703/996586-20170302151608485-747689090.png)
但是我们会发现添加人员的时候用户类型显示如下:
![](https://images2015.cnblogs.com/blog/996586/201703/996586-20170302151609923-1542139486.png)
这并不是我们想要的,我们可以在类中添加如下代码:
![](https://images2015.cnblogs.com/blog/996586/201703/996586-20170302151611548-1199399455.png)
这时我们就会发现,选项下拉框的变化
![](https://images2015.cnblogs.com/blog/996586/201703/996586-20170302151612798-481700528.png)
添加一个用户看看:
![](https://images2015.cnblogs.com/blog/996586/201703/996586-20170302151615282-2087319380.png)
数据库数据:
![](https://images2015.cnblogs.com/blog/996586/201703/996586-20170302151616985-8686897.png)