我尝试在这个Django模型上进行迁移/迁移:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
public class KeyBindingTest {
static JButton button1;
static JButton button2;
static JPanel panel;
public KeyBindingTest() {
panel = new JPanel(new BorderLayout());
JToolBar tb = new JToolBar();
tb.setFloatable(false);
button1 = new JButton("First Button");
button1.addActionListener(new ButtonAction());
button2 = new JButton("Second Button");
button2.addActionListener(new ButtonAction());
tb.add(button1);
tb.add(button2);
panel.add(tb, BorderLayout.PAGE_START);
JTextArea ta = new JTextArea(10, 30);
JScrollPane opts = new JScrollPane(ta);
panel.add(opts, BorderLayout.PAGE_END);
setKeyBindings(tb);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
KeyBindingTest test = new KeyBindingTest();
test.createAndShowUI();
}
});
}
private void setKeyBindings(JToolBar tb) {
tb.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_1, 0), "first");
tb.getActionMap().put("first", new ButtonAction());
tb.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_2, 0), "second");
tb.getActionMap().put("second", new ButtonAction());
}
private void createAndShowUI() {
JFrame frame = new JFrame();
frame.getRootPane().setDefaultButton(button1);
frame.setLayout(new BorderLayout());
frame.add(panel, BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public class ButtonAction extends AbstractAction {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(panel, "You pressed a button \n" + e.paramString());
}
}
}
我收到此错误:
from django.db import models
from myapp.models import Site
class GscElement(models.Model):
ctr = models.FloatField('Taux de clic', default=0.0)
impressions = models.IntegerField('Nombre d\'impressions', default=0)
position = models.FloatField('Position moyenne', default=0.0)
clicks = models.IntegerField('Nombre de clics', default=0)
site = models.ForeignKey(
Site,
models.SET_NULL,
blank=True,
null=True
)
class Page(GscElement):
page_field = models.TextField('Url de la page', default='')
startdate = models.DateField('Date du debut', null=True)
enddate = models.DateField('Date de fin', null=True)
class Meta:
unique_together = (('startdate', 'enddate', 'page_field',))
class Query(GscElement):
query_field = models.TextField('Requête', default='')
startdate = models.DateField('Date du debut', null=True)
enddate = models.DateField('Date de fin', null=True)
class Meta:
unique_together = (('startdate', 'enddate', 'query_field'),)
你知道为什么吗?
仅供参考我尝试恢复模型更改之前的数据库,并尝试压缩迁移,但总是会出现同样的错误。
谢谢!
答案 0 :(得分:2)
在没有看到迁移文件的情况下,我无法肯定地说。发布这些将有所帮助。也就是说,看起来你试图将字符串字段中的ctr或位置重命名为float字段。
这可能发生在你运行makemigrations并且它询问你是否重命名了一个字段时。如果您不关心这些字段中的数据,可以执行以下操作:
Django无法将字符串隐式转换为浮点数,因此如果您确实需要保留当前存储在模型中的数据,我建议如下: