[英]Django mptt, can I delete the default 'name' filed?
I'm following the django-mptt tutorial,and there is a 'name' filed:我正在关注 django-mptt 教程,并且有一个“名称”归档:
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:与我自己的 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?我的问题是我可以隐藏或忽略这个文件吗?
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.删除 name 字段时出现错误的原因是
order_insertion_by
选项 [readthedocs.io]引用了该字段。 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.
假定任何标识为定义排序的字段在数据库中永远不会是 NULL。
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']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.