On 25/06/2020 8:29 am, Jim Shepherd wrote:
> 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 pin 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)
>
Just a stab in the dark - have you tried giving from_contact a related_name?
>
> 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?
Another stab ... maybe you could just display the
ContactConnectionAdmin(admin.ModelAdmin) with model=ContactConnection?
I haven't advanced to 3.x so I don't know what it is capable of.
>
> 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+unsubscribe@googlegroups.com
> <mailto:django-users+unsubscribe@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
> <https://groups.google.com/d/msgid/django-users/91dad4bc-7eba-461d-89f1-aaf173e80831o%40googlegroups.com?utm_medium=email&utm_source=footer>.
--
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 view this discussion on the web visit https://groups.google.com/d/msgid/django-users/174d1c37-cc87-4015-89c0-83f325f8d8c1%40dewhirst.com.au.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment