I am unable to get a symmetrical, self-referencing ManyToManyField with a 
through table to actually be symmetrical, at least through the Admin 
interface. I am using Django 3.0 so it looks like the capability is 
supported.

Models:


class Contact(models.Model):
    last_name = models.TextField(default='', blank=True)
    connections = models.ManyToManyField('self',
                                         through='ContactConnection',
                                         symmetrical=True,
                                         blank=True)

    def get_connections(self):
        return ', '.join([str(p.last_name) for p in self.connections.all()])


class ContactConnection(models.Model):
    to_contact = models.ForeignKey(Contact,
                                   related_name='contacts',
                                   on_delete=models.CASCADE,
                                   null=False)
    from_contact = models.ForeignKey(Contact,
                                     on_delete=models.CASCADE,
                                     null=False)
    comments = models.TextField(default='', blank=True)




Admin:

class ContactConnectionInline(admin.TabularInline):
    model = Contact.connections.through
    fk_name = 'from_contact'
    extra = 0

@admin.register(Contact)
class ContactAdmin(admin.ModelAdmin):
    ordering = ('last_name',)
    list_display = ['last_name', 'get_connections']
    inlines = (ContactConnectionInline)


 
>From the Admin page, when viewing a Contact, only one-direction of the 
ContactConnection relationship is shown which makes sense due to the 
fk_name='from_contact attribute fixing the direction.
But it seems that at least the get_connections() method would get both 
sides.

Is there a way either through a query or preferably an Inline to show the 
Contacts on both sides of the ContactConnection?

Thanks,
Jim

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/91dad4bc-7eba-461d-89f1-aaf173e80831o%40googlegroups.com.

Reply via email to