Remarkably, I've just today tripped over this same exact
problem again.  Turns out even using a post_save signal
callback function does not provide the information about
the new M2M data.

# callback for post_save signal, debug code removed for brevity
def ldap_netgroup_save(sender, **kwargs):
    instance = kwargs['instance']
    newng = kwargs['created']
    cn = 'cn=%s,ou=Netgroup,%s' % (instance.name, base_dn)
    connection = ldap.initialize(ldap_uri)
    connection.simple_bind_s(bind_dn, bind_password)
    if newng:
        # WORKS
        connection.add_s(cn, [('objectClass', 'nisNetgroup'),])
    ifnames = []
    for i in instance.interfaces.all():
        ifnames = ifnames + make_nisnetgrouptriples(i.fqdn)
    # HERE 'ifnames' is INCORRECTLY []
    connection.modify_s(cn, [(ldap.MOD_REPLACE, 'nisNetgroupTriple',
                        ifnames),])

[NEW NETGROUP]*click*

Netgroup: foobar
Interfaces: I select 3
[SAVE]

DEBUG LOG:
Made new netgroup in LDAP: mynewnetgroup
    saving interfaces as []

I view the netgroup in the admin interface and sure enough, all 3
'interfaces' are associated properly.

If I just click 'save' then it reports what it should
have reported when I made the new netgroup all along:

DEBUG LOG:
Existing netgroup in LDAP: mynewnetgroup
    saving interfaces as ['(agilent,,)', '(agilent.our.org,,)',
    '(ape,,)', '(ape.our.org,,)', '(aprh6test,,)',
'(aprh6test.our.org,,)']

This is such nonsense :(

On Jan 12, 12:20 pm, Jeff <[email protected]> wrote:
> On Jan 11, 9:23 pm, Dennis Lee Bieber <[email protected]> wrote:
>
> > On Wed, 11 Jan 2012 17:26:44 +0000, Tom Evans <[email protected]>
> > wrote:
>
> > >On Wed, Jan 11, 2012 at 5:22 PM, Jeff <[email protected]> wrote:
> > >> When Device.netgroups (a M2M field) changes, we need to perform
> > >> some python-ldap operations.
>
> > >Have you considered simply going back to the database to check what
> > >the values currently are? It would be inefficient, but clean and
> > >concise.
>
> >         I've forgotten how many means of changing this data there are,
> > but...
>
> Dennis,
>
> Several.
>
> A web-only solution won't work.
>
> I found the 'm2m_changed' signal yesterday, read that you can't
> determine *what* changed by using it, and also ended up directed
> to some open bug reports... etc... and threw up my hands.
>
> To the best of my digging, there is no way to accurately tell
> in SomeModel.save() whether the previous value of MyM2MField
> and the new value are the same, and/or what the differences
> are.
>
> I ended up completely restructuring stuff (I'll spare you the
> details) so that I can just say "whatever .netgroups looks
> like AFTER save, make LDAP look exactly like that."  This
> is essentially what Tom Evans suggested in his reply.  It's
> inefficient, and clearly a workaround, but is at least doable.
>
> It boils down in LDAP-terms to saying "replace all attribute
> values for X with the following full data.  I don't care what
> your old data was."  ... instead of saying, "remove value Y
> from the 1000 values set on X"  :|
>
> Thank you all for your efforts to help.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to