Hey Mike,

I tried to replicate what you were trying to do and I got it working.

By the way, you can directly delete filtered child objects, no need to
create a list. And you don't need to create a list for iterating over a
queryset.

Queryset supports iteration by default(
https://docs.djangoproject.com/en/dev/topics/db/queries/#querysets-are-lazy
).

Here is models.py:

from django.db import modelsfrom django.db.models.signals import
pre_deletefrom django.dispatch import receiver

class Master(models.Model):
    name = models.CharField(max_length=200)

    def __unicode__(self):
        return self.name

class Parent(models.Model):
    name = models.CharField(max_length=200)
    master = models.ForeignKey(Master)
    mark = models.CharField(max_length=40)

    def __unicode__(self):
        return self.name

    def clean_out(self):
        Child.objects.filter(master=self.master, tags=self.mark).delete()

        child_lst = Child.objects.filter(master=self.master,
                                         tags__contains=self.mark)
        for child in child_lst:
            marks = child.tags.split()
            marks.remove(self.mark)
            child.tags = ' '.join(marks)
            child.save()

class Child(models.Model):
    name = models.CharField(max_length=200)
    tags = models.TextField()
    master = models.ForeignKey(Master)

    def __unicode__(self):
        return self.name

@receiver(pre_delete, sender=Parent)def parent_delete_handeler(sender,
instance, *args, **kwargs):
    instance.clean_out()


Sincerely,
Pankaj Singh
http://about.me/psjinx


On Tue, Feb 5, 2013 at 12:15 PM, Mike Dewhirst <mi...@dewhirst.com.au>wrote:

> Could someone please point out my mistake?
>
>     def clean_out(self):
>         child_lst = list(Child.objects.filter(**master=self.master,
>                                               tags=self.mark))
>         for child in child_lst:
>             child.delete()
>
>         child_lst = list(Child.objects.filter(**master=self.master,
>                                               tags__contains=self.mark))
>         for child in child_lst:
>             marks = child.tags.split()
>             marks = marks.remove(self.mark)
>             child.tags = ' '.join(marks)
>             child.save()
>
>
> The "self" in this case is an instance of the Parent class and clean_out()
> is a Parent model method. Both Parent and Child have a foreign key
> relationship with the Master model. Nothing has a foreign key pointing to
> either Parent or Child.
>
> Parent.clean_out() method is connected to the pre_delete signal of the
> Parent model.
>
> The purpose of the method is more or less documented below.
>
> I've tried this a number of different ways and always get the "Select a
> valid choice ..." error mentioned below.
>
>
> On 4/02/2013 6:14pm, Mike Dewhirst wrote:
>
>> This is the error ... Select a valid choice. That choice is not one of
>> the available choices.
>>
>> I want to delete a bunch of "child" records when a "parent" record is
>> deleted. While I'm using that terminology they are not actually related
>> in the usual sense. All however are in foreign key relationships with
>> the same owner/master.
>>
>> The child records all know which parents they have because the parent
>> added its mark to a field in the child when the child record got created
>> and linked to the master. In fact if that child had been added by
>> another parent, the next parent just adds its mark instead of adding
>> another child.
>>
>> When a parent gets deleted from the master, its mark is removed from all
>> the child records it was involved with and if it was the only mark the
>> child gets deleted too. Or at least that is what I want to happen.
>>
>> My question is:
>>
>> In overview, how should I be doing this?
>>
>> At the moment I'm trying to do it all via model methods calling various
>> managers because it feels like the right place to do it.
>>
>> I have tried to make a custom manager with use_for_related_fields = True
>> but still get the same error.
>>
>> Thanks for any help
>>
>> Mike
>>
>>
> --
> 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<django-users%2bunsubscr...@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?hl=en<http://groups.google.com/group/django-users?hl=en>
> .
> For more options, visit 
> https://groups.google.com/**groups/opt_out<https://groups.google.com/groups/opt_out>
> .
>
>
>

-- 
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+unsubscr...@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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to