小编ech*_*ion的帖子

如何将字段从一个模型移动到另一个模型,并仍然保留数据?

为了简化,我有一个模型,比方说,

class Parent(models.Model):
    field_a = models.CharField(max_length=40)
Run Code Online (Sandbox Code Playgroud)

和另一个包含图像字段的模型,它通过外键将自身绑定到父实例:

class ParentPicture(models.model):
    parent = models.ForeignKey(Parent)
    picture = models.ImageField(upload_to=upload_path)
    is_used = models.BooleanField(default=False)
Run Code Online (Sandbox Code Playgroud)

最初的计划是为父母支持多张图片,并且在任何时候只使用一张图片,但现在,我想支持一张图片,并将其包含在父模型中,这样ParentPicture就可以了被毁坏了,Parent看起来像这样:

class Parent(models.Model):
    field_a = models.CharField(max_length=40)
    picture = models.ImageField(upload_to=upload_path)
Run Code Online (Sandbox Code Playgroud)

我不确定将picture字段移动到新模型的最佳方法,并包含ParentPicture具有is_used标志的实例.

是否有一种简单的方法可以使用Django自动执行此操作,或者我是否需要对Parent模型进行更改,迁移更改,然后运行脚本以完成ParentPicture模型并进行相应的复制,然后在完成之后,删除该ParentPicture模型?

感谢您的帮助/建议!

python django django-models

7
推荐指数
2
解决办法
2336
查看次数

在C#中,是否可以在后台打开URL而无需打开浏览器?

我的代码需要通过php脚本向服务器提供一些信息.

基本上我想打电话,www.sitename.com/example.php?var1=1&var2=2&var3=3但我不希望浏览器打开,所以Process.Start(URL);不会工作.

由于我来到这个网站学习而不是得到答案,大多数情况下,我会解释到目前为止我所做的事情以及我得到的错误.如果你知道一个解决方案,请随意跳过下一部分.

我环顾四周,看到了使用POST的解决方案:

ASCIIEncoding encoding=new ASCIIEncoding();
string postData="var1=1&var2=2&var3=3";
byte[]  data = encoding.GetBytes(postData);

// Prepare web request...
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://localhost/site.php");
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream=myRequest.GetRequestStream();

// Send the data.
newStream.Write(data,0,data.Length);
newStream.Close();
Run Code Online (Sandbox Code Playgroud)

但是,我需要使用的GET不是POST.起初我以为解决办法是改变myRequest.Method = "POST";GET,但这并没有工作,因为那不是如何GET工作,它从URL中提取数据.

那么,我试图将以前的代码更改为:

HttpwebRequest myRequest= (HttpWebRequest)WebRequest.Create("http://localhost/site.php" + postData);
Stream newStream = myRequest.GetRequestStream();
newStream.Close()
Run Code Online (Sandbox Code Playgroud)

根据它会调用URL的逻辑,它会(希望)在php脚本上启动GET_请求,然后生活就会花花公子.但是这会导致以下错误:

A first chance exception of type 'System.Net.ProtocolViolationException' occurred in System.dll
An unhandled exception of type …
Run Code Online (Sandbox Code Playgroud)

c# winforms .net-4.5

6
推荐指数
1
解决办法
8239
查看次数

使用HyperlinkedModelSerializer强制https?

我正在使用REST框架用于API,并且与HyperlinkModelSerializer的关系由使用http的URL表示.有没有办法强迫它返回https的链接?

python django django-rest-framework

6
推荐指数
1
解决办法
282
查看次数

如何将perl脚本打包为32位可执行文件?

我在64位Windows 7机器上,我一直PAR:Packager用来打包我的脚本.exe.

但是,我现在需要.exe32位Windows 7机器上运行这些.

无论如何将它打包为64位机器的32位可执行文件?(有可能PAR:Packager吗?)

windows perl executable windows-7-x64

5
推荐指数
1
解决办法
779
查看次数

如何在长时间运行的功能中更新UI(文本字段)?

我知道这个问题可能没有意义,而且我正在努力想办法解释它,所以我将展示一段代码来帮助.我在visual studio express 2010上使用Winforms:

private void button1(object sender, EventArgs e)
    {
        txtOutput.Text += "Auto-collecting variables. This may take several minutes";
        string v = foo();
        txtOutput.Text += "\n" + v;
        string b = bar();
        txtOutput.Text += "\n" + b;

        txtOutput.SelectionStart = txtOutput.Text.Length;
        txtOutput.ScrollToCaret(); //scrolls to the bottom of textbox
    }
Run Code Online (Sandbox Code Playgroud)

所以基本上,当用户点击button1时,我希望"自动收集变量..."显示在文本框中,然后让foo()执行,显示它,然后执行bar(),然后显示.

当前正在发生的是foo()和bar()执行,然后在执行foo()和bar()之后一切都显示(需要几分钟的函数).无论如何要解决这个问题,还是有解决方法?

编辑:C#的版本是4.0.如果我更新到4.5或5.0,没有.NET 4.5/5.0的计算机能够运行.exe吗?

c# .net-4.0 winforms

3
推荐指数
1
解决办法
4385
查看次数

在bash或shell中,如何在不创建临时文件的情况下将一行文本作为文件输入?

问题1:

我正在使用一个自制的命令,由于原因,无法更改.该命令需要一个它将读取的文件名,如下所示:read-cmd "testtext.txt"我知道可以使用文件作为输入重定向的某些命令的输入流,例如some-cmd < "text.txt",但我想知道相反的情况是否正确,我是否可以使用文本行和使bash相信它是一个文件,所以我能够做到read-cmd "contents of what should be in a text file"

我唯一能做的就是

Example 1:
echo "contents of what should be in a text file" > tmptextfile
read-cmd "tmptextfile"
rm tmptextfile
Run Code Online (Sandbox Code Playgroud)

但是,我真的不愿意这样做,而只是简单地将该行传输,就好像它是一个文件一样.有没有可能的方法来做到这一点,还是完全依赖于如何read-cmd运作?

问题2:

但是,一个非常类似的问题,它不是文件作为命令的输入,而是命令选项的输入.所以,read-cmd2 -d "testtext.txt" ...

Example 2:
echo "contents of what should be in options text file" > tmpoptfile
read-cmd2 -d tmpoptfile ...
rm tmpoptfile
Run Code Online (Sandbox Code Playgroud)

bash input

3
推荐指数
1
解决办法
68
查看次数

基于多个键中的一个,在pandas中对一系列的值求和?

我在python中使用pandas,我有一个pandas Series对象,我不能为我的生活弄清楚.它基本上是这样的:

>>>print(series_object)

key1              key2      key3                                                             
First class       19438     Error1:0       117
                  16431     Error2:0       80
                  1         Error3:0       70
Second class      28039     Error4:0       65
Third class       2063      Error5:0       28
                  19439     Error6:0       25
Fourth class      25975     Error7:0       11
Fifth class       23111     Error8:0       7
                  1243      Error9:665     4
                            Error9:581     3
                  27525     Error10:0      3
                  1243      Error9:748     2
                  1247      Error11:65     2
                  1243      Error9:852     2
                  1247      Error11:66     2
                            Error11:70     1
                            Error11:95     1
                            Error11:181    1
                            Error11:102    1
                            Error11:160    1
Run Code Online (Sandbox Code Playgroud)

我想要一种方法来对key2匹配的这个对象的值求和,以便它变为series_object:

>>>print(series_object)
key1              key2      key3                                                             
First class       19438 …
Run Code Online (Sandbox Code Playgroud)

python pandas

3
推荐指数
1
解决办法
5712
查看次数

Python:有没有办法强制父函数返回?

在我的程序中,我有一个处理requests调用的函数,并返回已处理的调用或引发异常.许多其他函数使用此函数,但是,我遇到的问题是如何处理可能引发的异常.目前它设置如此(简化):

def getFromAPI(url):
    # create variable headers
    r = requests.get(url, headers=headers)
    if r.status_code == 404:
        raise Exception("Error raised")
    else:
        #process data
        return data

def functionone():
    ...
    try:
        data = getFromAPI(url)
    except Exception as e:
        return handleException(e) 
        #handles problems, returns exception-formatted data

    ...
    # formatting data specific to functionone
    return formatted_data

def functiontwo():
    ...
    try:
        data = getFromAPI(url)
    except Exception as e:
        return handleException(e) 
        #handles problems, returns exception-formatted data

    ...
    # formatting data specific to functiontwo
    return formatted_data

def …
Run Code Online (Sandbox Code Playgroud)

python exception

3
推荐指数
1
解决办法
1611
查看次数