0

I'm following the django-mptt tutorial,and there is a 'name' filed:

class Genre(MPTTModel):
    name = models.CharField(max_length=50, unique=True)
    parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='children')

    class MPTTMeta:
        order_insertion_by = ['name']

compare to my own model:

class Comment(MPTTModel):
    """
    评论表
    """
    name = models.CharField(max_length=1500, unique=True,)
    nid = models.AutoField(primary_key=True)
    news = models.ForeignKey(verbose_name='评论文章', to='News',to_field='id',on_delete=models.CASCADE)
    user = models.ForeignKey(verbose_name='评论者', to='User',to_field='id',on_delete=models.CASCADE)
    content = models.CharField(verbose_name='评论内容', max_length=255)
    create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='children')

    class MPTTMeta:
        order_insertion_by = ['name']

This 'name' filed is really bothers me, since each time I need to add this filed.

My question is can I hidden or ignore this filed?

William
  • 3,724
  • 9
  • 43
  • 76
  • You don't need the `name`. The `order_insertion_by` is simply how to order it, but you can remove the name and the `order_insertion_by`. – Willem Van Onsem Mar 03 '21 at 21:32
  • You thus can use the primary key for example over the name as `order_insertion_by`. – Willem Van Onsem Mar 03 '21 at 21:34
  • Thank you guys now I understand why when I deleted the name filed there was error that is because I forget to modify the order_insertion_by. – William Mar 03 '21 at 21:38

1 Answers1

1

The reason that you got an error when you removed the name field is because the order_insertion_by option [readthedocs.io] was referring to that field. But you can let it refer to another field. As the documentation says:

order_insertion_by

A list of field names which should define ordering when new tree nodes are being inserted or existing nodes are being reparented, with the most significant ordering field name first. Defaults to [].

It is assumed that any field identified as defining ordering will never be NULL in the database.

You can thus for example make use of the primary key instead:

class Genre(MPTTModel):
    parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='children')

    class MPTTMeta:
        order_insertion_by = ['pk']
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • Thank you for your reply, one more question,how can I limit the max deep of the recursetree? – William Mar 03 '21 at 21:52
  • @William: you can create a field `level` that is a `PositiveIntegerField`, but where you set a database constraint that it is less than a given value, then you register that field as `level_attr` in the `MPTTMeta`: https://django-mptt.readthedocs.io/en/latest/models.html#model-options – Willem Van Onsem Mar 03 '21 at 21:54
  • Hi friend I really appreciate your help, can you help reply me here. https://stackoverflow.com/questions/66465571/how-to-limit-the-max-deep-of-recursetree-in-django-mttp – William Mar 03 '21 at 22:03