我有一个抽象的模型类UploadItem来处理上传的文件.我希望每个子类能够定义upload_to路径.为此,我将回调传递给FileField的构造函数.
这是一个例子:
class UploadItem(models.Model):
file = models.FileField(upload_to=UploadItem.get_directory)
class Meta:
abstract = True
# I want videos to be storred in 'videos/' directory
class Video(UploadItem):
def get_directory(self, instance, filename):
return 'videos/'
Run Code Online (Sandbox Code Playgroud)
但这不起作用,我收到此错误:
file = models.FileField(upload_to=UploadItem.get_directory)
NameError: name 'UploadItem' is not defined
Run Code Online (Sandbox Code Playgroud) 我为C++ 11编写了一个小实用程序类,我将其用作范围保护,以便更轻松地处理异常安全和类似的事情.
看起来有点像黑客.但我很惊讶我没有在使用C++ 11功能的其他地方看到它.我认为boost与C++ 98类似.
但这是个好主意吗?或者是否有我错过的潜在问题?在boost或类似的解决方案中是否已经有类似的解决方案(使用C++ 11功能)?
namespace detail
{
template<typename T>
class scope_exit : boost::noncopyable
{
public:
explicit scope_exit(T&& exitScope) : exitScope_(std::forward<T>(exitScope)){}
~scope_exit(){try{exitScope_();}catch(...){}}
private:
T exitScope_;
};
template <typename T>
scope_exit<T> create_scope_exit(T&& exitScope)
{
return scope_exit<T>(std::forward<T>(exitScope));
}
}
#define _UTILITY_EXIT_SCOPE_LINENAME_CAT(name, line) name##line
#define _UTILITY_EXIT_SCOPE_LINENAME(name, line) _UTILITY_EXIT_SCOPE_LINENAME_CAT(name, line)
#define UTILITY_SCOPE_EXIT(f) const auto& _UTILITY_EXIT_SCOPE_LINENAME(EXIT, __LINE__) = ::detail::create_scope_exit(f)
Run Code Online (Sandbox Code Playgroud)
并且使用了类似的东西.
int main ()
{
ofstream myfile;
myfile.open ("example.txt");
UTILITY_SCOPE_EXIT([&]{myfile.close();}); // Make sure to close file even in case of exception
myfile << "Writing …Run Code Online (Sandbox Code Playgroud) 有谁知道如何添加一个带有"actionlinkwithimage"的确认对话框?
自动调整后是否可以获得最终字体大小?(属性adjustsFontSizeToFitWidth设置为YES,文本字体大小正在缩小以适合标签)
我在UILabel中继承了drawTextInRect以在文本上放置渐变,但是渐变大小需要与字体的大小相同.我无法获得适当大小的调整字体......它甚至可能吗?
//draw gradient
CGContextSaveGState(myContext);
CGGradientRef glossGradient;
CGColorSpaceRef rgbColorspace;
size_t num_locations = 2;
CGFloat locations[2] = { 0.0, 1.0 };
CGFloat components[8] = { 1, 1, 1, 0.25, // BOTTOM color
1, 1, 1, 0.12 }; // UPPER color
//scale and translate so that text would not be rotated 180 deg wrong
CGContextTranslateCTM(myContext, 0, rect.size.height);
CGContextScaleCTM(myContext, 1.0, -1.0);
//create mask
CGImageRef alphaMask = CGBitmapContextCreateImage(myContext);
CGContextClipToMask(myContext, rect, alphaMask);
rgbColorspace = CGColorSpaceCreateDeviceRGB();
glossGradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations);
//gradient should be sized …Run Code Online (Sandbox Code Playgroud) 我一直在使用Eclipse内存分析工具来检查堆转储.我还没有看到任何一个对象通过线程堆栈中的局部变量保持活动的情况.
堆转储中是否保留了java线程堆栈?如果没有,这些对象是否被计为转储中无法访问的对象?如果是这样,有没有办法保留线程堆栈,以便可以将未收集的垃圾与局部变量值区分开来?
我需要转换许多不同的对象,并且我想避免为每个对象编写转换器类。每个对象都继承自一个基类,我需要使用 Id 来获取描述(这是在我对 CacheManager 的调用中处理的)。
对于每个类(我有大约 30 个类),我编写了以下代码:
object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Dictionary<int, string> codes = CacheManager.CodeLookup<CourtEventCode>();
int id = 0;
string result = string.Empty;
if (int.TryParse(value.ToString(), out id) && id > 0)
{
if (codes.ContainsKey(id))
{
result = codes[id];
}
else
{
result = "Unknown";
}
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,CourtEventCode代表这一类的转换器。有没有一种方法可以从 IValueConverter.Convert 的 targetType 输入派生该类,而不必基本上复制并粘贴该类两打?
预先感谢,
桑尼
在Hibernate或其他ORM中实现复合主键时,最多有三个位置将insertable = false,updatable = false放在使用标识关系的复合主键星座中(FK是PK的一部分):
第三种是使用@IdClass和JPA 1.0 AFAIK的唯一方法.请参阅http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Primary_Keys_through_OneToOne_Relationships.我只会考虑案例1.和2.
问:将"insertable = false,updatable = false"置于一般的首选位置是哪种方式?
我遇到过关于这个问题的Hibernate问题.例如,Hibernate 3.5.x会抱怨Zips表
CREATE TABLE Zips
(
country_code CHAR(2),
code VARCHAR(10),
PRIMARY KEY (country_code, code),
FOREIGN KEY (country_code) REFERENCES Countries (iso_code)
)
Run Code Online (Sandbox Code Playgroud)
有:
org.hibernate.MappingException: Repeated column in mapping for entity: com.kawoolutions.bbstats.model.Zip column: country_code (should be mapped with insert="false" update="false")
org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:676)
org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:698)
...
Run Code Online (Sandbox Code Playgroud)
如您所见,country_code列是PK和FK.这是它的类:
实体类:
@Entity
@Table(name = "Zips")
public class Zip implements Serializable
{
@EmbeddedId
private ZipId id;
@ManyToOne …Run Code Online (Sandbox Code Playgroud) 我有一个文件加载到流阅读器中.该文件包含分散的IP地址.在每个IP地址之前和之后,如果有帮助,则为"\".此外,每行的第一个"\"始终在IP地址之前,在第一个之前没有其他"\".
我已经知道我应该使用while循环遍历每一行,但我不知道其余的过程:<
例如:
由Stormix.de提供支持\ 93.190.64.150\7777\False
Cupserver\85.236.100.100\8178 \假
Euro Server\217.163.26.20\7778\False
在第一个例子中我需要"93.190.64.150"在第二个例子中我需要"85.236.100.100"在第三个例子中我需要"217.163.26.20"
我真的很难解析/拼接/切割:s
提前致谢
***我要求将IP保留在一个字符串中,bool返回不足以满足我的要求.
我们正在尝试从cvs迁移到git.
我们的目标是1)完美的当前代码库2)可用的历史记录.如果它在旧分支中缺少一个条目,我们就不在乎了.
两条评论:
即使使用cvs存储库的本地副本,也需要很长时间.干运行超过24小时(7.5 gb cvs代码库; P4 2.0 ghz机器,2 GB RAM,10K驱动器.Git将其压缩到1.8 gb).因为CVS本身占用了cpu的99%,我想它是cpu绑定的.有什么方法可以加快速度吗?
Stdout有一堆警告.哪些重要?哪些不?
WARNING: Invalid PatchSet 5763, Tag pre-merge-pe-2-3-merge-26:
conf/peTEST.conf:1.4=after, src/java/com/participate/util/XSLUtilTEST.java:1.1=before. Treated as 'before'
revision 1.167.2.11 of file derived/workflow/xml/bpd.xml is tagged but not present
revision 1.106 of file derived/workflow/xml/bpd.xml is tagged but not present
Branch PE-2-3 already exists!
Warning: commit message does not conform to UTF-8.
提前致谢
将
我正在使用这个类向网站发出一个 GET 和另一个 POST 请求(第一个请求是设置一个 cookie)。我正在使用来自 wampserver dot com 的 wamp 在带有 virtualbox 的 Win XP 虚拟机中进行测试。这 2 个请求需要 10 到 18 秒(使用 curl),但是如果我直接通过同一虚拟机中的 webbrowser 发出这些请求,则网站只需几秒钟即可加载,并且它会检索所有图像、css 等。
是什么导致 curl 工作如此缓慢?有办法解决吗?