Answer the question
In order to leave comments, you need to log in
How to override object names in forms.ModelMultipleChoiceField in Django Admin?
Models:
class PostTag(models.Model):
title = models.CharField(max_length=255)
class Post(models.Model):
title = models.CharField(max_length=255)
post_tag = models.ManyToManyField(PostTag, null=True, blank=True)
class PostAdmin(ModelAdmin):
filter_horizontal = ('post_tag',)
Answer the question
In order to leave comments, you need to log in
https://docs.djangoproject.com/en/1.7/ref/contrib/...
there are many examples there. In short, you create a new function for the model that returns the amount. Add allow_tags. We go to the admin panel and write lists_display. Everything.
True, I did not use manytomany. For foreign keys only.
I decided to check, everything is working fine. Only not quite the same, but the principle is clear.
models.py
from django.utils.html import format_html
class PostTag(models.Model):
title = models.CharField(max_length=255)
def __str__(self):
return self.title
def get_count_posts(self):
return format_html('<span style="color: red">{0}</span>', self.post_set.count())
get_count_posts.allow_tags = True
class Post(models.Model):
title = models.CharField(max_length=255)
post_tag = models.ManyToManyField(PostTag, null=True, blank=True)
def __str__(self):
return self.title
from games.models import PostTag, Post
class PostTagAdmin(admin.ModelAdmin):
list_display = ('title', 'get_count_posts')
admin.site.register(PostTag, PostTagAdmin)
admin.site.register(Post)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question