我有一个依赖于元组的Django模型.我想知道在我的Django程序中引用该元组中的常量的最佳实践是什么.例如,在这里,我想将" default=0
" 指定为更具可读性且不需要评论的东西.有什么建议?
Status = (
(-1, 'Cancelled'),
(0, 'Requires attention'),
(1, 'Work in progress'),
(2, 'Complete'),
)
class Task(models.Model):
status = models.IntegerField(choices=Status, default=0) # Status is 'Requires attention' (0) by default.
Run Code Online (Sandbox Code Playgroud)
编辑:
如果可能的话,我想完全避免使用一个数字.不知何故,使用字符串'需要注意'将更具可读性.
在互联网上研究这个之后,我一直无法让Eclipse索引器解决GCC 4.4.4附带的C++ 0x附加内容中的"shared_ptr".我确保用Eclipse的正确包含创建我的项目,所以它肯定在查看4.4.4包含文件夹.
程序编译并运行得很好.要访问shared_ptr我正在使用"#include <memory>".
知道是什么打破了索引器吗?
我有一个Django模型字段,我想内联.该领域是一种多对多的关系.所以有"项目"和"用户档案".每个用户配置文件可以选择任意数量的项目.
目前,我有"表格式"内联视图工作.有没有办法有一个"水平过滤器",以便我可以轻松地添加和删除用户配置文件中的项目?
请参阅附图以获取示例.
这是用户配置文件的型号代码:
class UserProfile(models.Model):
user = models.OneToOneField(User, unique=True)
projects = models.ManyToManyField(Project, blank=True, help_text="Select the projects that this user is currently working on.")
Run Code Online (Sandbox Code Playgroud)
和项目的模型代码:
class Project(models.Model):
name = models.CharField(max_length=100, unique=True)
application_identifier = models.CharField(max_length=100)
type = models.IntegerField(choices=ProjectType)
account = models.ForeignKey(Account)
principle_investigator = models.ForeignKey(User)
active = models.BooleanField()
Run Code Online (Sandbox Code Playgroud)
以及视图的管理代码:
class UserProfileInline(admin.TabularInline):
model = UserProfile.projects.through
extra = 0
verbose_name = 'user'
verbose_name_plural = 'users'
class ProjectAdmin(admin.ModelAdmin):
list_display = ('name', 'application_identifier', 'type', 'account', 'active')
search_fields = ('name', 'application_identifier', 'account__name')
list_filter = ('type', 'active')
inlines …
Run Code Online (Sandbox Code Playgroud) 我一直在尝试获取在jsTree中选择的节点的文本.我能够填充树并触发onSelect事件,但我找不到单击的节点.我在网上看到了data.rslt.obj.attr("data")
用于获取文本的示例,但这对我来说是未定义的.此外,当我尝试使用所选节点时,.jstree('get_selected')
我无法在对象中的任何位置找到节点文本.如何获取节点文本?
这是我的onSelect回调函数:
function onSelect(event, data)
{
// Get the name of the equipment that was selected.
var selected_node = $("#equipment_tree").jstree('get_selected');
var equipment_name = data.rslt.obj.attr("data");
}
Run Code Online (Sandbox Code Playgroud) 我想确定网卡是否已启用,启动和插入.基本上,我想知道网卡是否可以正常工作.我需要使用C++程序提供此信息,并希望在网络无法正常工作时显示错误消息.如果可能,我想避免使用shell命令来确定此信息.
我在网页上有一个下拉框,使用HTML5 <select>
标记.有时我想让它"只读",即只显示文本.例如:
有时我希望它是"读/写":
有时我希望它是"只读":
我宁愿不使用"禁用"属性.我觉得它看起来很俗气,并暗示当用户没有可用时,会以某种方式选择用户.
是否可以select
使用CSS 将当前选项的外观转换为普通文本?
我有两个型号:
class A(models.Model):
name = models.CharField(max_length=100, unique=True)
class B(models.Model):
a = models.ForeignKey(A)
Run Code Online (Sandbox Code Playgroud)
在Django中,我如何选择具有指向它们的B类对象的类'A'的所有对象?例如,如果数据库包含类'A'的这三个条目:
A, named "one"
A, named "two"
A, named "three"
Run Code Online (Sandbox Code Playgroud)
B类的两个条目:
B, points to "two"
B, points to "three"
Run Code Online (Sandbox Code Playgroud)
我想选择A类"两"和"三".
我已经编写了一些单元测试来测试我的Django应用程序.特别是一个测试套件在其setUp()
功能中有很多代码.所述代码的目的是为数据库创建测试数据.(是的,我知道固定装置,并选择在这种情况下不使用它们).当我运行单元测试套件时,第一个运行的测试通过,但随后套件中的其余测试失败.所有失败的消息都是相同的:它提到错误的位置是"self.database_object.save()",原因是"IntegrityError:列名不唯一".所以,我最好的猜测是Django在每次测试后都没有正确拆除数据库.
今天早些时候它正在工作,但我想我做了一些重构搞砸了.关于为什么Django在每次测试后没有正确拆除数据库的任何想法?
我使用asio :: streambuf遇到了问题,我希望有人可以告诉我,如果我错误地使用了这个类.当我运行这个示例代码时,它会出现段错误.为什么?
为了使事情更加混乱,此代码适用于Windows(Visual Studio 2008),但不适用于Linux(使用gcc 4.4.1).
#include <boost/asio.hpp>
using namespace std;
int main()
{
boost::asio::streambuf Stream;
// Put 4 bytes into the streambuf...
int SetValue = 0xaabbccdd;
Stream.sputn(reinterpret_cast<const char*>(&SetValue), sizeof(SetValue));
// Consume 3 of the bytes...
Stream.consume(3);
cout << Stream.size() << endl; // should output 1
// Get the last byte...
char GetValue;
// --------- The next line segfaults the program ----------
Stream.sgetn(reinterpret_cast<char*>(&GetValue), sizeof(GetValue));
cout << Stream.size() << endl; // should output 0
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有一个通过Kerberos使用远程身份验证的网站.我根据Django文档(https://docs.djangoproject.com/en/dev/howto/auth-remote-user/)设置了所有内容.登录效果很好,但是注销不会结束会话.我该怎么做才能确保用户完全注销?完全关闭浏览器窗口似乎确实有效,但用户需要能够在不关闭浏览器窗口的情况下注销.
django ×5
python ×3
c++ ×2
boost ×1
boost-asio ×1
c++11 ×1
css ×1
django-admin ×1
eclipse ×1
html ×1
html5 ×1
javascript ×1
jstree ×1
kerberos ×1
linux ×1
streambuf ×1
unit-testing ×1