简体   繁体   中英

limiting the number of displayed instances of a model in Django Admin

I have a model which over timer will have several thousand to tens of thousands of instances. Each of those is unique. In the Django Admin interface I can see all the instances and access them etc. Is there a way for me to limit the number of instances I can see to lets say 20 or 50 sorted in whichever order and I can look through them like pages? I ask because loading all several thousand instances will likely clog up my server. So how do I limit it?

My model is as follows if that helps:

#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)

You can set the number of items with list_per_page in your ModelAdmin: https://docs.djangoproject.com/en/1.11/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_per_page

The ordering within the admin by default is as defined by in the models options: https://docs.djangoproject.com/en/1.11/ref/models/options/#ordering . For custom ordering, you can specify the ordering on your ModelAdmin (see: ModelAdmin.ordering in the docs)

You have to set the every single Field to be truncated:

eg i want to truncate field: 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)

so in admin.py

class ArticleAdmin(admin.ModelAdmin):
    fields = ('title', 'description', 'content', 'id_type',)
    list_display = ('id', 'title', 'slug', 'short_content',)

so the field "content will be truncated to less than or equal 200 characters"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM