我有一个模型,超过计时器将有几千到几万个实例。每一个都是独一无二的。在Django Admin界面中,我可以看到所有实例并访问它们。有没有办法让我限制我可以看到的实例数量,让20或50按照任何顺序排序,我可以像页面一样查看它们?我问,因为加载几千个实例可能会堵塞我的服务器。那么我该如何限制呢?
如果有帮助,我的模型如下:
#Student Type
class StudentType(models.Model):
name = models.CharField(max_length = 400, null=True, blank = True, verbose_name = 'Name')
def __unicode__(self):
return self.name
#Old -- Deprecated:Student Images Folder
def student_directory_path(instance, filename):
return 'students/%s' % filename
#Student Images Folder
def st_profile(instance, filename):
# file will be uploaded to MEDIA_ROOT/user_<id>/<filename>
return 'students/%s' % filename
#Student Profile
class ProfileStudent(models.Model):
user = models.OneToOneField(app_settings.USER_MODEL,)
created = models.DateTimeField(auto_now=False, auto_now_add=True, blank = False, null = False, verbose_name = 'Creation Date')
email = models.EmailField(max_length=254, blank=True, null=True)
name = models.CharField(max_length = 400, null=True, blank = True, verbose_name = 'Name')
last_name = models.CharField(max_length = 400, null=True, blank = True, verbose_name = 'Last Name')
phone_number = models.CharField(max_length = 12, null=True, blank = True, verbose_name = 'Phone Number')
city = models.ForeignKey(City, null=True, blank = True, verbose_name = 'City')
adress = models.CharField(max_length = 400, null=True, blank = True, verbose_name = 'Address')
bank_account = models.CharField(max_length = 400, null=True, blank = True, verbose_name = 'Bank Account')
braintree_submerchant_account = models.CharField(max_length = 400, null=True, blank = True, verbose_name = 'Braintree Account')
date_of_birth = models.DateField(auto_now=False, auto_now_add=False, blank = True, null = True, verbose_name='Date of birth')
GENDER = (
('male','Male'),
('female','Female'),
)
gender = models.CharField(max_length=20, choices=GENDER, blank = True, null=True, verbose_name = 'Gender' )
language = models.ManyToManyField(Language)
student_type = models.ForeignKey(StudentType, null = True, blank = True, verbose_name='Type')
profile_image = models.ImageField(upload_to=st_profile, blank = True, null = True, default='/perfil.png')
is_embassador = models.BooleanField(default=False)
is_new = models.BooleanField(default=True)
# True if user from the old web:
legacy = models.BooleanField(default=False, blank=True)
legacy_id = models.IntegerField(default=0, null=True, blank=True)
# Used to invite other users: created at signup
invite_code = models.ForeignKey(PromotionalCode, default=None, blank=True, null=True)
class Meta:
verbose_name_plural = 'Students'
verbose_name = 'Student'
def __unicode__(self):
return "%s" % (self.user.email)
def save(self, *args, **kwargs):
if not self.pk:
invite_code = PromotionalCode()
invite_code.code = uuid.uuid4().hex[:6].upper()
try:
invite_code.pc_type = PromotionalCodeType.objects.all().filter(pk=1).get()
except:
invite_code.pc_type = None
invite_code.save()
self.invite_code = invite_code
super(ProfileStudent, self).save(*args, **kwargs)
答案 0 :(得分:1)
您可以在ModelAdmin中设置list_per_page
项目的数量:https://docs.djangoproject.com/en/1.11/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_per_page
默认情况下,admin中的排序如模型选项中所定义:
https://docs.djangoproject.com/en/1.11/ref/models/options/#ordering。
对于自定义排序,您可以在ModelAdmin上指定ordering
(请参阅:文档中的ModelAdmin.ordering)
答案 1 :(得分:0)
您必须设置要截断的每个字段:
例如,我想截断字段:content
# in model
from django.template.defaultfilters import truncatechars
class Article(models.Model):
"""infor for manage every single news"""
title = models.CharField(max_length=150, default='')
slug = models.SlugField(max_length=150, null=True)
description = models.CharField(max_length=255)
content = models.CharField(max_length=5000)
@property
def short_content(self):
"""Use this for truncating content field"""
return truncatechars(self.content, 200)
如此在admin.py
class ArticleAdmin(admin.ModelAdmin):
fields = ('title', 'description', 'content', 'id_type',)
list_display = ('id', 'title', 'slug', 'short_content',)
因此字段“内容将被截断为少于或等于200个字符”