简体   繁体   中英

Django Admin list_per_page for a multichoicefield m2m

I have a m2m relationship setup and it works on the admin side. however it lists all 60,000 records (as it should), to make this work more efficiently when loading how do I limit the number of returned records. I have looked into the list_per_page option to add to the admin.py but this seems to work for the object list, I need to have this in the multiselectfield.

Any ideas?

# models.py

class Sample(models.Model):
    sample_id = models.AutoField(primary_key=True)
    area_easting = models.IntegerField(choices = EASTING_CHOICES)
    area_northing = models.IntegerField(choices = NORTHING_CHOICES)
    context_number = models.IntegerField()
    sample_number = models.IntegerField()
    sample_type = models.CharField(max_length=200, default='', blank=True, null=True, choices = MATERIALS)
    weight = models.DecimalField(max_digits=6, decimal_places=2)
    description = models.CharField(max_length=500, default='', blank=True, null=True)
    recovery_method = models.CharField(max_length=200, default='', blank=True, null=True, choices = RECOVERY_METHODS)
    taken_by = models.ForeignKey(settings.AUTH_USER_MODEL, db_column='taken_by', on_delete = models.PROTECT, related_name='depotsample_taken_by')
    comments = models.CharField(max_length=1000, default='', blank=True, null=True)

    def __str__(self):
        return str(self.sample_number)


    class Meta:
        db_table = 'kap\".\"sample'
        #ordering = ["sample_id"]
        managed = True
        #verbose_name_plural = "samples"

class Container(models.Model):
    container_id = models.AutoField(primary_key=True)
    container_name = models.CharField(max_length=50, blank=True, null=True)
    container_type = models.CharField(max_length=50, blank=True, null=True)
    location_id = models.ForeignKey(Location, db_column='location_id', on_delete = models.PROTECT)

    samples = models.ManyToManyField('Sample')
    icon_desc = models.ForeignKey(Icon, db_column='icon_desc', null=True, blank=True, default='Box',on_delete = models.PROTECT)


    def __str__(self):
        return self.container_name

The admin side:

# admin.py

class ContainerAdmin(admin.ModelAdmin):
    list_display = ('container_name',)
    search_fields = ['container_name']
    filter_horizontal = ('samples',)
    list_per_page = 5 # No of records per page

class SampleAdmin(admin.ModelAdmin):
    list_display = ('sample_number',)
    search_fields = ['sample_number']
    list_per_page = 5 # No of records per page 


admin.site.register(Container, ContainerAdmin)
admin.site.register(Sample, SampleAdmin)

You could modify the queryset of the ModelMultipleChoiceField.

class ContainerAdminForm(ModelForm):
    class Meta:
        model = Container
        fields = '__all__'

    def __init__(self, *args, **kwargs):
        form = super().__init__(*args, **kwargs)
        # Limit samples to 10
        self.fields['samples'].queryset = Sample.objects.all()[:10]

class ContainerAdmin(admin.ModelAdmin):
    list_display = ('container_name',)
    search_fields = ['container_name']
    filter_horizontal = ('samples',)
    form = ContainerAdminForm

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