Django admin list_display not showing multiple objects

I just started doing admin views in Django, and for starters I'm trying to do something very simple: showing a few fields in the list of objects using list_display, as described here: https://docs.djangoproject.com/en/dev/ref / contrib / admin /

This is my dead simple code:

class ArticleAdmin(admin.ModelAdmin): list_display = ('title', 'category') 

Unfortunately, the list_display parameter causes a column view to appear, but only some of the objects (40 of 85) are now displayed in the list. I can’t understand why some objects appear above others - their fields look as if they are filling in the same way. This is clearly not pagination, because when I tried it with the administrator of a different model, it showed only 2 objects from about 70 objects.

What could be here?

[UPDATE] Article Model:

 class Article(models.Model): revision = models.ForeignKey('ArticleRevision', related_name="current_revision") category = models.ForeignKey('meta.Category') language = models.ForeignKey('meta.Language', default=get_default_language) created = models.DateTimeField(auto_now_add=True, editable=False) changed = models.DateTimeField(auto_now=True, editable=False) title = models.CharField(max_length=256) resources = models.ManyToManyField('oer.Resource', blank=True) image = models.ManyToManyField('media.Image', blank=True) views = models.IntegerField(editable=False, default=0) license = models.ForeignKey('license.License', default=get_default_license) slug = models.SlugField(max_length=256) difficulty = models.PositiveIntegerField(editable=True, default=0) published = models.NullBooleanField() citation = models.CharField(max_length=1024, blank=True, null=True) 

Before adding list_display:

Django amdin before list_display

After adding list_display:

Django amdin after list_display

[UPDATE] This behavior only occurs when the ForeignKey fields are included in the list_display tuple. Any of them.

[UPDATE] Model model code:

 class Category(models.Model): title = models.CharField(max_length=256) parent = models.ForeignKey('self') project = models.NullBooleanField(default=False) created = models.DateTimeField(auto_now_add=True, editable=False) slug = models.SlugField(max_length=256, blank=True) def __unicode__(self): return self.title 
+4
source share
1 answer

This behavior is caused by a foreign key relationship somewhere that is not declared nullable but nonetheless has a null value in the database. When you have the ManyToOne relationship in list_display, the change list class will always execute the query using select_related. (See the get_query_set method in django.contrib.admin.views.ChangeList).

select_related by default follows all foreign keys for each object, so any broken foreign key found by this request will lead to data loss when analyzing the request. This does not apply to the administrator; you can check it online by comparing the results of the article .objects.all () with Article.objects.all (). select_related ().

There is no easy way to manage foreign keys that the administrator will look for - select_related accepts some parameters, but the administrator does not provide a way to pass them. Theoretically, you can write your own ChangeList class and override get_query_set, but I do not recommend this.

The real solution is to make sure that the foreign key model fields accurately reflect the state of your database in zero settings. Personally, I will probably do this by commenting all the FK on an article that is different from the category, and see if that helps, and then returning them one by one until everything starts to break. The problem is not related to the FK article itself; if there is a broken FK in the editorial, language or category, which will still lead to the failure of the missing lines. Or, if something they refer to has a broken FK, etc. Etc.

+14
source

Source: https://habr.com/ru/post/1479029/


All Articles