Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to order django-mptt tree by DateTimeField?

This is the model I am using:

class Comment(MPTTModel):
    comment = models.CharField(max_length=1023)
    resource = models.ForeignKey('Resource')
    created_at = models.DateTimeField(auto_now_add=True)
    parent = TreeForeignKey('self', null=True, blank=True, related_name='children')
    author = models.ForeignKey(User)

    class MPTTMeta:
        order_insertion_by = ['created_at']

However, when I try to add a comment from the admin site I get:

ValueError at /admin/app/comment/add/
Cannot use None as a query value

Am I doing something wrong with my model? I feel like django-mptt is trying to get the DateTimeField while it is still "None", before it has been set at the db level.

like image 972
WSkinner Avatar asked Mar 07 '12 05:03

WSkinner


1 Answers

No, you're not doing something wrong. This is a bug in django-mptt.

Basically datetime fields with auto_add_now=True don't get a value until after django-mptt tries to figure out where to insert your model in the tree.

I've just created an issue on django-mptt to fix this: https://github.com/django-mptt/django-mptt/issues/175

In the meantime, you can work around this by proactively setting the value yourself. Get rid of the auto_now_add=True, and set the value in an overridden save() method on your model::

from datetime import datetime

class Comment(MPTTModel):
    comment = models.CharField(max_length=1023)
    resource = models.ForeignKey('Resource')
    created_at = models.DateTimeField()
    parent = TreeForeignKey('self', null=True, blank=True, related_name='children')
    author = models.ForeignKey(User)

    class MPTTMeta:
        order_insertion_by = ['created_at']

    def save(self, *args, **kwargs):
        if not self.created_at:
            self.created_at = datetime.now()
        super(Comment, self).save(*args, **kwargs)
like image 178
craigds Avatar answered Oct 02 '22 10:10

craigds