One more reply after doing some more research. Apparently this has
been discussed quite a bit and a solution may appear in the 1.3
release:
http://code.djangoproject.com/ticket/7539

But, to handle the situation where you want to avoid a cascade delete
(which was my problem with my previous/next linked list of chapters),
override the delete method on the class:
def delete(self):
    self.previous_set.clear()
    self.next_set.clear()
    super(Chapter,self).delete()

This works only if the ForeignKey can be null, so for example, the
fields look like this:
previous = models.ForeignKey('self',
blank=True,null=True,related_name='a')
next = models.ForeignKey('self',
blank=True,null=True,related_name='b')

Note however, I haven't tested this code yet, caveat emptor. From what
I can glean from the docs and the forums though, it's close to
correct.

thanks,
--Tim

On Jun 15, 1:41 pm, Tim Arnold <a_j...@bellsouth.net> wrote:
> Thanks for both these great answers. After thinking about it, I think
> either way I go I'll need to modify both the add and delete methods.
> The linked list seems more natural to me, but since my writers will be
> using the interface, the sequence number may be a better choice for
> them.  More to think about...
>
> thanks again!
> --Tim
>
> On Jun 15, 12:48 pm, Dan Harris <dih0...@gmail.com> wrote:
>
>
>
> > Hi Tim,
>
> > You can probably override the Chapter model's delete method. Inside
> > this overridden delete method you can swap around your FK's of
> > previous and next chapters before calling the super classes delete.
> > Basically do your linked list management inside the delete method
> > before the calling the super class delete. This way when the cascades
> > go through it doesn't have links to next or previous chapters and not
> > everything is deleted.
>
> > Hope this helps,
>
> > Dan Harris
> > dih0...@gmail.com
>
> > On Jun 15, 12:11 pm, Tim Arnold <a_j...@bellsouth.net> wrote:
>
> > > Hi,
> > > I have a model for a Book that contains Chapters. My problem is
> > > figuring out the ordered sequence of Chapters in a Book. I first tried
> > > setting Chapter up as a linked list with pointers to the previous
> > > Chapter and next Chapter. That worked okay, but when I need to delete
> > > a Chapter, the prev/next links cascade through and it wants to delete
> > > all the chapters.
>
> > > I'm now thinking of adding a 'sequence number' to the chapter model
> > > which I suppose will work okay, but if want to add or delete a chapter
> > > later on, I'll have to renumber all the later chapters in the
> > > database.
>
> > > This is a little tricky--does anyone have a better solution?
> > > thanks,
> > > --Tim Arnold

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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