How to order django-mptt tree by DateTimeField?

2019-06-20 18:14发布

问题:

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.

回答1:

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)