I’ve tried to accomplish what I want both by overriding the save method in my model, and by trying various iterations of signals, and nothing seems to work.
I have a Member model with a many2many relationship to a Link model. I add Links to a Member record using a TabularInline in the admin form for a Member.
This is an abbreviated view of my code:
# models.py
from django.db.models.signals import m2m_changed
from django.dispatch import receiver
class Link(models.Model):
link = models.URLField(max_length=200, unique=True)
...
class Member(models.Model):
links = models.ManyToManyField('Link', blank=True)
link_count = models.SmallIntegerField(default=0)
...
@receiver(m2m_changed, sender=Member.links.through)
def update_counter(sender, instance, action, reverse, model, pk_set, **kwargs):
if action == 'post_add':
instance.link_count = instance.links.count()
instance.save()
# admin.py
admin.site.register(Link)
class LinkInline(admin.TabularInline):
model = Member.links.through
...
@admin.register(Member)
class MemberAdmin(DjangoQLSearchMixin, admin.ModelAdmin):
exclude = ['links', ]
inlines = [LinkInline,]
...
After I defined my most recent iteration of a m2m_changed signal, then added another Link to a sample Member record, the link_count
in the relevant Member record was updated one time, yet it was erroneously updated to a count that would’ve only been correct prior to the record edit that added additional Links. Moreover, each subsequent edit of that same Member record is now leaving the link_count
unchanged from the first erroneous number.
I don’t know if there is a proper way to achieve what I want to do, which is to update the link_count
field on an affected Member record after any update to that Member record, using an accurate count of all related Link records. Maybe the correct solution lies beyond signals, or overriding the save method (which itself also yielded an erroneous, pre-edit link_count
).
Or are many-to-many relationships, specifically, something that is mostly impossible to base any sort of auto-updating scheme upon?
I do get an accurate count using a method in the admin, and I can sort on this calculated column in the admin, but I cannot do a search along the lines of link_count > 3
in the admin for example, so if I want to do such a search, it seems I do need to have a persisted field in the Member model. I just seem to have no good way of updating that persisted field automatically.