0%

Django admin

引子

问题1:django admin下拉列表不显示值,显示为object的处理

处理办法是修改models.py,原来的model:

1
2
3
4
5
6
7
8
9
class TechnicistLocation(models.Model):  # 技术人员位置  
LocationName = models.CharField('位置名称', max_length=20)

class Meta:
verbose_name_plural = '技术人员位置'
app_label ="schedule"

def __unicode__(self):
return self.LocationName

修改后

1
2
3
4
5
6
7
8
9
class TechnicistLocation(models.Model):  # 技术人员位置  
LocationName = models.CharField('位置名称', max_length=20)

class Meta:
verbose_name_plural = '技术人员位置'
app_label ="schedule"

def __str__(self):
return self.LocationName

区别就在倒数第二行,python3直接使用str(self)就可以了,
如果是python2,则要用unicode(self)

问题2 忘记Admin密码怎么办?

进入Shell窗口

1
2
python manage.py shell

1
./manage.py shell

对Admin用户修改密码

1
2
3
4
from django.contrib.auth.models import User  
user =User.objects.get(username='admin')
user.set_password('new_password')
user.save()

如果忘记了用户名

1
2
3
4
from django.contrib.auth.models import User
user1 = User.objects.filter(is_superuser = True)
user2 = User.objects.filter(is_superuser = True, is_staff = True)
print( user1, user2)

注意:默认情况下,只有是staff和superuser的双重身份才能进去django自带的admin管理后台并进行修改和管理。

欢迎关注我的其它发布渠道