python - How to order django-mptt tree by DateTimeField? -
this model 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 try add comment admin site get:
valueerror @ /admin/app/comment/add/ cannot use none query value
am doing wrong model? feel django-mptt trying datetimefield while still "none", before has been set @ db level.
no, you're not doing wrong. bug in django-mptt.
basically datetime fields auto_add_now=true
don't value until after django-mptt tries figure out insert model in tree.
i've created issue on django-mptt fix this: https://github.com/django-mptt/django-mptt/issues/175
in meantime, can work around proactively setting value yourself. rid of auto_now_add=true
, , set value in overridden save() method on 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)
Comments
Post a Comment