Django ManytoMany not showing in admin

Viewed 1851

As my models are in ManyToMany relationship so connection should be in both ways, but are not.

For example in the admin for the BusinessProfile section I am able to see the name and image fields of Services model as they are in a ManyToMany relationship, but this not the case for the opposite. I am unable to see BusinessProfile models field in Services model in admin.

Is my model structure correct?

I have also attached images.BusinessProfile view in admin Services view in admin

models.py

class Service(models.Model):
    name = models.CharField(max_length=50)
    image = models.ImageField(upload_to='image', blank = True)
    #business_profile = models.ManyToManyField("BusinessProfile", blank=True, related_name="business_of_services")

    def __str__(self):
        return "Service - {}".format(self.name)


class BusinessProfile(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, on_delete=models.CASCADE)
    business_name = models.CharField(max_length=100, unique =True)
    register_date = models.DateTimeField(default=timezone.now)
    pan_number = models.IntegerField(unique=True)
    pan_name = models.CharField(max_length=100)
    address = models.TextField(max_length=200)
    pincode = models.IntegerField()
    city = models.CharField(max_length=50)
    state = models.CharField(max_length=50)

    service  = models.ManyToManyField("Service", blank=True, related_name="services_of_business")
    image  = models.ManyToManyField("Service", related_name="image_of_business")

    def __str__(self):
        return "Business - {}".format(self.business_name)
1 Answers

If you want to use reversed many to many admin, you can use inlines. See this document.

Example code in document.

from django.contrib import admin

class MembershipInline(admin.TabularInline):
    model = Group.members.through

class PersonAdmin(admin.ModelAdmin):
    inlines = [
        MembershipInline,
    ]

class GroupAdmin(admin.ModelAdmin):
    inlines = [
        MembershipInline,
    ]
    exclude = ('members',)
Related