Sunday, May 20, 2018

Re: Please I need help on how to create a custom method for a model with list_display

Thanks very much. I have solved the problem. 

On Sunday, May 20, 2018, Daniel Germano Travieso <danielgtravieso@gmail.com> wrote:
Hello!

On your case, as stated on the official doc, you should just create a method on the model you want to display the ManyToMany field on the list_display that maybe returns the list of items from the ManyToMany field. Django avoids using this as it could be a potential overhead as you will run seperate queries on the database to fetch all the related many-to-many objects.

But if you are feeling adventurous, just create the method that performs that query.

i.e. On your Book model you create a method that returns the self.authors.all() set. Then you use that method's name on the list_display instead of authors.

Hope it helps.

On Saturday, May 19, 2018 at 3:54:17 PM UTC-3, Godson Rapture wrote:

Please I am learning Django 2.0.4


Right now I am learning about Django Admin.


I got an error that the value of my list_display should not be a ManyToMany Field.


I referred back to Django official doc and I was asked to create a custom method for my model.


I tried looking for an example of how to do that, I could not see.


Here is my model.py program

 from django.db import models    class Publisher(models.Model):      name = models.CharField(max_length=30)      address = models.CharField(max_length=50,blank=True)      city = models.CharField(max_length=60,blank=True)      country = models.CharField(max_length=50,blank=True)      website = models.URLField()      def __str__(self):          return self.name    class Author(models.Model):      salution = models.CharField(max_length=10)      first_name = models.CharField(max_length=30)      last_name = models.CharField(max_length=40)      email = models.EmailField()      headshot = models.ImageField(upload_to = "tmp")      def __str__(self):          return f"{self.first_name} {self.last_name}"    class Book(models.Model):      title = models.CharField(max_length=100)      authors = models.ManyToManyField(Author)      publisher = models.ForeignKey(Publisher,on_delete=models.CASCADE,)      publication_date = models.DateField()      def __str__(self):          return self.title  

Here is my admin.py program

from django.contrib import admin  from polls.models import Book, Publisher, Author  # Register your models here.    class BookAdmin(admin.ModelAdmin):      list_display = [          "title",          "authors", #a ManyToMany Field        ]       search_fields = [          "Book"      ]      class Meta:          model = Book    admin.site.register(Book, BookAdmin)  

Can someone please help me.

Thanks.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/56c58fb6-7074-47aa-9536-8d8027ef70a0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAEUEpOR37QtBN72gczc9JKteiB5rBryL3vhXP1capNtc1UsLZQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment