How to order django-mptt tree by DateTimeField?

喜你入骨 提交于 2019-12-10 14:54:10

问题


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)


来源:https://stackoverflow.com/questions/9596115/how-to-order-django-mptt-tree-by-datetimefield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!