So, I've gotten a bit of code up and running so you can see a little
of what I have. I've pared everything down to just focus on the MPTT
bit:
class Comment(models.Model):
#core fields
text = models.TextField(blank = "True", null = "True")
#left and right nodes for traversal
left = models.IntegerField(blank = "True", null = "True")
right = models.IntegerField(blank = "True", null = "True")
and in another file:
from django.db import models
from testmodel.models import Comment
def insert_comment(parent_id, text):
"""
This method has to do several things:
-It has to create a space the comment to be save in the
correct spacing between left and right. So, it takes the parent
comment, and moves all other comments greater than
parent_comment.right to the right 2 numbers
-It figures out what the indentation level is:
parent comment indentation +1
-After the space has been created, the comment can then be
saved with left and right values between the values of the parent
comments left and right values.
"""
# Get all comments in correct order
# The MPTT algorithm ensures that when the comments are ordered by
'left' value
# they are in correct threaded order.
tree=Comment.objects.all().order_by('left')
#obtaining the parent of the comment to be inserted
parent=tree.get(pk=parent_id)
# Because of the MPTT algorithm, insertions require a re-ordering
of the tree.
# iterating through the tree shifts all the left/right nodes to
the
# right of parent.right over 2 values.
for node in tree:
if node.left > parent.right:
node.left += 2
if node.right >= parent.right:
node.right += 2
node.save()
# a new node is created with correct left/right values
Comment.objects.create(
text = text,
left = parent.right,
right = parent.right+1)
I'll post a method to display the comments with proper indentation in
a bit.
On Oct 6, 2:06 am, Jonathan Nelson <[EMAIL PROTECTED]> wrote:
> @pihentagy: I've looked at it, and the code, but it's not I need, and
> I wasn't able to get my head around the code that was there to be able
> to adapt it to what I needed.
>
> @Chis: I found that article this week. Thanks. I'm actually porting
> some of that code for my own project. I'll post a message when get
> further along in my app.
>
> Also, I'm a little bit of a newb to Django myself (5 months of steady
> work with it), but I wasn't really able to figure out how to use theMPTTapp,
> either. That's the reason that I'm writing my own.
>
> At DjangoCon, about 50% of the people attending actually said that
> they had rolled their own comments framework. So, I don't think we're
> alone.
>
> On Oct 5, 12:58 pm, "Chris Stromberger" <[EMAIL PROTECTED]>
> wrote:
>
> > This article is a great resource as well--explains all you need to know very
> > nicely I think (I especially like the trick of finding number of descendants
> > (so number of comments associated with an item) just from inspecting the
> > values for "right" and "left"--single row query). I found this a while back
> > and had started rolling my own, then I discovered Django (so have started
> > "porting" my site to Django), then I discovered thempttapp and thought I
> > could just drop it in. Has not been that easy (probably in large part due
> > to my Django-newbie
> > status).http://www.sitepoint.com/article/hierarchical-data-database/2/
>
> > Keep us posted on your system. Would like to see it.
>
> > On Sat, Oct 4, 2008 at 11:02 AM, Jonathan Nelson <[EMAIL PROTECTED]>wrote:
>
> > > I'm actually rolling my own threaded comments system using anMPTT
> > > algorithm. Contrib.comments and the django-mpttaren't a really good
> > > fit for what I need.
>
> > > It's taken me a while to get my head around the concept of traversing
> > > the tree using the comment.left and comment.right values, but after
> > > you understand it, it really seems to work well.
>
> > > I'll try and put some code up on Google code when I get mine
> > > finished.
>
> > > I've found this article to be very helpful:
> > >http://dev.mysql.com/tech-resources/articles/hierarchical-data.html
>
> > > And, I've found this book to be invaluable:
>
> > >http://www.amazon.com/Hierarchies-Smarties-Kaufmann-Management-System...
>
> > > J.
>
> > > On Oct 1, 2:10 pm, "Chris Stromberger" <[EMAIL PROTECTED]>
> > > wrote:
> > > > If I can get something working, I'll share! Still fiddling around with
> > > it.
>
> > > > On Wed, Oct 1, 2008 at 2:48 PM, john Moylan <[EMAIL PROTECTED]> wrote:
> > > > > Hi Chris,
>
> > > > > I'd love to see your finished code - if you can/want to share?
>
> > > > > J
>
> > > > > 2008/9/30 Chris Stromberger <[EMAIL PROTECTED]>
>
> > > > > OK, nevermind on the admin page issue. Was being caused
> > > > >> by TEMPLATE_STRING_IF_INVALID being set to something other than ''.
> > > > >> Would still love to see an example of contrib.comments +mptt!
>
> > > > >> Thanks,
> > > > >> Chris
>
> > > > >> On Tue, Sep 30, 2008 at 4:25 PM, Chris Stromberger <
> > > > >> [EMAIL PROTECTED]> wrote:
>
> > > > >>> One specific issue I am having is that the admin screen for the
> > > Comment
> > > > >>> model is not "synched up" with the data. The admin list screen has
> > > > >>> a
> > > table
> > > > >>> of field names, and the columns do not match the data--the data is
> > > shifted.
> > > > >>> So for example, the "Is public" column is displaying the IP
> > > > >>> address.
> > > Etc.
> > > > >>> Any ideas on what would cause this? Mpttadds some fields to your
> > > model
> > > > >>> dynamically, so wondering if this is somehow related.
> > > > >>> Any tips appreciated.
>
> > > > >>> Thanks,
> > > > >>> Chris
>
> > > > >>> On Tue, Sep 30, 2008 at 2:31 PM, Chris Stromberger <
> > > > >>> [EMAIL PROTECTED]> wrote:
>
> > > > >>>> I'm trying to usempttwith contrib.commments and am not having much
> > > > >>>> luck. Basically would like to allow nested comments. Pretty new
> > > > >>>> to
> > > Django,
> > > > >>>> so that is contributing to the problem. Would love to see a simple
> > > > >>>> example/summary of what needs to change in contrib.comments to
> > > incorporate
> > > > >>>>mptt.
> > > > >>>> Thanks,
> > > > >>>> Chris- Hide quoted text -
>
> > - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---