Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

M2M through #151

Open
stygmate opened this issue Aug 3, 2024 · 0 comments
Open

M2M through #151

stygmate opened this issue Aug 3, 2024 · 0 comments

Comments

@stygmate
Copy link

stygmate commented Aug 3, 2024

i'm in the case of computed field depending on fields on a model on the otherside of M2M relation and also of fields in intermediate model.
(I have followed the documentation example)

updates are ok in all cases i tested except when doing: person.groups.add(group)
Meanwhile person.groups.remove(group) works.
( I suspect delete signal to be triggered in the case of remove but nothing other than m2m signal in the case of add )

any elegant solution to handle that without adding groups in depends (which trigger more updates than necessary) ?

excerpt from doc example for reference:

class Person(ComputedFieldsModel):
    name = models.CharField(max_length=32)

    @computed(models.CharField(max_length=256),
        depends=[
            ('memberships', ['joined_at']),
            ('memberships.group', ['name'])         # replaces groups.name dep
        ],
        prefetch_related=['memberships__group']
    )
    def groupjoins(self):
        if not self.pk:
            return ''
        names = []
        for membership in self.memberships.all():   # not using groups anymore
            names.append('{}: joined at {}'.format(
                membership.group.name, membership.joined_at))
        return ','.join(names)

class Group(models.Model):
    name = models.CharField(max_length=32)
    members = models.ManyToManyField(Person, related_name='groups', through='Membership')

class Membership(models.Model):
    person = models.ForeignKey(Person, related_name='memberships')
    group = models.ForeignKey(Group, related_name='memberships')
    joined_at = SomeDateField(...)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant