that old "django deleted my object!" thing
plus solution (at bottom)

class Venue(models.Model):
    pass

class Event(models.Model):
    venue =
models.ForeignKey(Venue,related_name='venue',blank=True,null=True)

v = Venue.objects.create()
e = Event.objects.create(venue=v)
v.delete()

the Event e is now deleted from the db
any model with a ForeignKey will be deleted with the model it points to is
deleted.
which is exactly what ForeignKeys are supposed to do

but my desired behavior here is to set:  e.venue = None
aka ON DELETE SET NULL

its a weak relationship

btw. this is not the solution, to flip it around:

class Venue(models.Model):
    events = models.ManyToManyField(Event,blank=True)

because in actual practice there are many ForeignKeys pointing to models in
different apps
and it would be horribly polluting to put ManyToManyFields in all of those.
in these cases these are more like attributes or extra information.
(tho in most cases ForeignKey is correct behavior)


but the best solution I think is to write a :

class WeakForeignKey(ForeignKey):
    """which can only be blank=True/null=True """"
    ...

class WeakManyToOneRel(ManyToOneRel):
    pass


django.db.models.base

    def _collect_sub_objects(self, seen_objs, parent=None, nullable=False):
    ...
            if isinstance(related.field.rel, OneToOneRel):
                ...
            elif isinstance(related.field.rel,WeakManyToOneRel):
                for sub_obj in getattr(self, rel_opts_name).all():
                    setattr(sub_obj,related.field.name,None)
                    sub_obj.save()
            else:
                ...


class Event(models.Model):
    venue = models.WeakForeignKey(Venue)


which I did, it works, test written etc.

comments ?  prior art that I didn't find ?
easier solutions ?
naming advice ?

I'll happily write up a ticket and patch




     felix :    crucial-systems.com

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

Reply via email to