Friday, January 30, 2015

Re: Changing many-to-many using Admin Site never calls post_remove Signal

not very nice, and probably not pythonic (still learning) but now I added another two for loops to find old references and delete them:
    if action == "post_add":
       
if isinstance(instance, Database):
           
# then update pairs
           
for a in instance.audioData():
               
for q in instance.questions.all():
                    pair
= AudioQuestionPair.objects.get_or_create(audioData=a, question=q, database=instance)
                   
for choice in q.get_choices():
                        answer
= Answer.objects.get_or_create(body=choice, audioQuestionPair=pair[0])
       
# check for removed questions and delete pairs
        pairs
= AudioQuestionPair.objects.filter(database=instance)
        toDelete
= []
       
for pair in pairs:
           
if pair.question not in instance.questions.all():
                toDelete
.append(pair)
       
for item in toDelete:
            item
.delete()



On Friday, January 30, 2015 at 9:44:29 PM UTC+1, Tobias Dacoir wrote:
I have a many-to-many relationship which the Admin can change using the Admin Site. I use a ModelAdmin.filter_horizontal, but even with the default widget it's the same.

I registered a receiver when m2m_changed, however it always only calls: pre_clear, post_clear, pre_add, post_add, in that order.

Now for post_add I have code to execute, to create new objects in the database, however when a value is removed from this many-to-many field, I need to delete some objects. Since pre_remove is never called, I can't tell which value was removed and I'm unable to find out which objects to delete.

Is there a way to modify the Admin page to actually call remove / add on the Model? Or I need another way to find out which value was removed.

--
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/83eb139a-3732-4986-ad8a-12e36e9c8c1c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment