我应该如何在 django admin 中表示位标志 int 字段?
我有一个数据模型,其位字段定义如下:
alter table MemberFlags add column title varchar(50) not null default '';
alter table MemberFlags add column value integer( 3) not null default 0;
insert into MemberFlags (title, value) values
("Blacklisted", 1),
("Special Guest", 2),
("Attend Ad-hoc Sessions", 4),
("Attend VIP Sessions", 8),
("Access Facility A", 16),
("Access Facility B", 32)
并像这样使用:
alter table Membership add column title varchar(50) not null default '';
alter table Membership add column flags integer( 3) not null default 0;
insert into Membership (title, flags) values
("Guest Pass", 4+2 ),
("Silver Plan", 16+ 4 ),
("Gold Plan", 32+16+ 4+2 ),
("VIP Pass", 32+16+8+4+2 )
我的问题是:
A)将不同位标志表示为管理站点中单独项目的最简单方法是什么?我应该覆盖模板,还是对表单做一些事情?
B) 搜索列表怎么样?我可以在模型中创建函数来表示每一位,但是如何进行搜索和排序呢?
我是姜戈的新手。
I have a data model with a bitfield defined something like this:
alter table MemberFlags add column title varchar(50) not null default '';
alter table MemberFlags add column value integer( 3) not null default 0;
insert into MemberFlags (title, value) values
("Blacklisted", 1),
("Special Guest", 2),
("Attend Ad-hoc Sessions", 4),
("Attend VIP Sessions", 8),
("Access Facility A", 16),
("Access Facility B", 32)
And used like this:
alter table Membership add column title varchar(50) not null default '';
alter table Membership add column flags integer( 3) not null default 0;
insert into Membership (title, flags) values
("Guest Pass", 4+2 ),
("Silver Plan", 16+ 4 ),
("Gold Plan", 32+16+ 4+2 ),
("VIP Pass", 32+16+8+4+2 )
My questions are:
A) What's the easiest way to represent the different bitflags as separate items in the admin site? Should I override the template, or do something with forms?
B) How about the search list? I could create functions in the model to represent each bit, but how would searching and sorting be done?
I'm new to Django.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一个经过测试的优秀解决方案,即使它不能立即适合您的模型,也可以使用 django-bitfield
A great tested solution, even if it doesn't fit your model right away, would be using django-bitfield
我认为最好的解决方案是通过子类化
models.Field
来创建新的字段类型。您可以使用 choices 参数来分配有效位标志及其含义。这将有助于保持模型声明的整洁和可读性,最终结果如下:Django 文档有一篇关于如何子类化模型的深入文章。字段:
编写自定义模型字段
它似乎涵盖了您需要做的所有事情,包括:
如果您正在寻找子类字段的示例,此片段可能有用。它的目标是相似的(多个选择作为模型字段),但将它们存储在数据库中的方式不同(它使用 CSV 文本字段而不是位标志)。
I think the best solution here would be for you to create a new field type by subclassing
models.Field
. You could make use of the choices parameter to assign the valid bit flags and their meanings. This would help keep your model declaration clean and readable, with a final result along the lines of:The Django documentation has a great in-depth article on how to go about subclassing models.Field:
Writing Custom Model Fields
It seems to cover everything you need to do, including:
If you're looking for an example of a subclassed field, this snippet might be of use. Its goal is similar (multiple choices as a model field), but its manner of storing them in the database differs (it's using a CSV text field instead of bit flags).
根据安德鲁的回答中的片段,您需要进行以下更改:
Working off the snippet in Andrew's answer, here are the changes you'd need to make:
这就是我在 User 类中使用标志的方式:
当然,您可以创建一个以逗号分隔的字符串来显示在管理视图中,或者您想要的任何内容,而不是打印标志。
对于管理员,只需为每个使用 布尔值团体价值观。
This is how I would use the flags with my User class:
Of course instead of printing the flags, you can create a comma-separated string to display in the admin view, or whatever you desire.
For the admin, just use a boolean for each of the group values.