Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 June 15, 2010 17:11:08

Tim A.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

modeling a book repository


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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 June 15, 2010 17:46:26

Daniel R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

modeling a book repository


On Jun 15, 5: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

Well having a sequence number is the right way to go. Not sure how
often you'd need to insert a chapter, but renumbering is fairly
simple:

Chapter.objects.filter(number__gte=insertednum).update(number=F(number)
+1)
--
DR.

--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 June 15, 2010 17:48:43

Dan H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

modeling a book repository


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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 June 15, 2010 18:41:50

Tim A.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

modeling a book repository


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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#5 June 16, 2010 15:43:00

Tim A.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

modeling a book repository


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/7539But, 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 athttp://groups.google.com/group/django-users?hl=en.

Offline

Board footer

Moderator control

Enjoy the 15th of December
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support