我是django和这个网站的新手,所以很抱歉,如果以前已经解决了这个问题,但是我没有找到
所以我有2个Django模型
ModelA(Model):
ModelB(Model):
modelA = ForeignKey(ModelA, on_delete=models.CASCADE)
ModelB的表格
ModelBForm(ModelForm):
class Meta:
model=ModelB
exclude=()
和一个视图
createModelBView(CreateView):
model = ModelB
form_class = ModelBForm
仅模板
{{form}}
渲染后,在ModelA字段中有一个下拉列表,因此我可以从ModelA的现有实例中进行选择,但是如果需要创建一个新实例,该怎么办?在管理员中,有一个选项可以在弹出窗口中编辑或创建新的ModelA。是否可以使用CreateView执行此操作?
谢谢
答案 0 :(得分:0)
在管理员中,有一个选项可以在弹出窗口中编辑或创建新的ModelA。是否可以使用CreateView执行此操作?
不,不是内置的。管理员中的功能涉及许多前端工作,涉及模板和路由,这些工作必须来自某个地方。由于Form / ModelForm实例不能假定它具有对管理员的访问权限(这是一个contrib模块,可能未启用,并且是权限敏感的),因此不能假定为此所需的基础结构可用一般情况。
请记住,<?php
$registers = array(
"zero", "at", "v0", "v1", "a0", "a1", "a2", "a3", "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "s0",
"s1", "s2", "s3", "s4", "s5", "s6", "s7", "t8", "t9", "k0", "k1", "gp", "sp", "fp", "ra"
);
$typeR = array(
"add" => 32, "sub" => 34, "sll" => 0, "srl" => 2, "and" => 36, "or" => 37, "xor" => 38, "slt" => 42, "mul" => 24,
"div" => 26, "jr" => 8
);
$typeI = array(
"addi" => 8, "lui" => 15, "andi" => 12, "ori" => 13, "xori" => 14, "slti" => 10, "beq" => 4, "bne" => 5, "lw" => 35,
"sw" => 43
);
$typeJ = array(
"j" => 2, "jal" => 3
);
function typeR($address, $rs, $rt, $rd, $registers, $shamt, $typeR, $opfunc)
{
if ((empty($rd) || !ctype_digit($shamt)) && $opfunc != "jr") {
return -1;
}
return array(
"address" => $address,
"bin" => "000000" . sprintf("%05d", decbin(array_search($rs, $registers)))
. sprintf("%05d", decbin(array_search($rt, $registers)))
. sprintf("%05d", decbin(array_search($rd, $registers)))
. sprintf("%05d", decbin($shamt)) . sprintf("%06d", decbin($typeR[$opfunc]))
);
}
function typeI($address, $rs, $rt, $imm, $registers, $typeI, $opfunc)
{
$is_hex = strpos($imm, "x");
$imm = $is_hex ? hexdec(substr($imm, $is_hex + 1)) : $imm;
$imm = ($imm < 0) ? substr(decbin($imm), -16) : sprintf("%016b", $imm);
return array(
"address" => $address,
"bin" => sprintf("%06d", decbin($typeI[$opfunc]))
. sprintf("%05d", decbin(array_search($rs, $registers)))
. sprintf("%05d", decbin(array_search($rt, $registers))) . $imm
);
}
function typeJ($address, $addr, $typeJ, $opfunc)
{
return array(
"address" => $address,
"bin" => sprintf("%06d", decbin($typeJ[$opfunc])) . sprintf("%026d", decbin($addr))
);
}
function blankLines($string)
{
$string = str_replace(array("\0", "\t", "\r", " ", "\n"), "", $string);
return (strlen($string) ? 1 : 0);
}
function findLabel($array, $arrayLen, $label)
{
$label = $label . ":";
for ($i = 0; $i < $arrayLen; $i++) {
$array[$i] = explode(" ", $array[$i]);
$subarrayLen = count($array[$i]);
for ($j = 0; $j < $subarrayLen; $j++) {
if (!strcmp($label, trim($array[$i][$j]))) {
return ($i * 4);
}
}
}
return -1;
}
function comments($string)
{
$commentI = ($string[0] == "#") ? -1 : strpos($string, "#");
return (($commentI) ? substr($string, 0, $commentI - strlen($string)) : $string);
}
function format($array)
{
$spaces = array(" ", " ", " ", " ", " ", " ", " ", " ", " ");
$label = "";
for ($i = 0; $i < count($array); $i++) {
$array[$i] = str_replace(",", " ", $array[$i]);
$array[$i] = @strtolower(trim(str_replace($spaces, " ", comments($array[$i]))));
$inc = blankLines($array[$i]);
$labelF = strpos($array[$i], ":");
$instr = $labelF ? substr($array[$i], $labelF + 1) : $array[$i];
if (!$inc || (!strlen($instr) && $i < (count($array) - 2))) {
if (!strlen($instr) && $labelF) {
$label = str_replace(array("\0", "\t", "\r", " ", "\n"), "", $array[$i]);
}
unset($array[$i]);
$array = array_values($array);
$i--;
} elseif (isset($label)) {
$array[$i] = ($label . " " . trim($array[$i]));
unset($label);
$array = array_values($array);
}
}
return $array;
}
function fileWrite($file, $instEnd, $count)
{
$arrayLen = count($instEnd);
for ($i = 0; $i < $arrayLen; $i++) {
$instEnd[$i]["bin"] = substr($instEnd[$i]["bin"], -8, 8) . "+"
. substr($instEnd[$i]["bin"], -16, 8) . "+"
. substr($instEnd[$i]["bin"], -24, 8) . "+"
. substr($instEnd[$i]["bin"], -32, 8);
$instEnd[$i]["bin"] = explode("+", $instEnd[$i]["bin"]);
$aux = 0;
foreach ($instEnd[$i]["bin"] as $key => $value) {
$instEnd[$i]["bin"][$key] = sprintf("%02X", bindec($value));
$str = "0x" . sprintf("%03X", ($instEnd[$i]["address"] + $aux)) . " 0x" . $instEnd[$i]["bin"][$key];
$str .= (((($count - 1) * 4) - 1) == (($i * 4) + $aux)) ? "\0" : "\n";
fwrite($file, $str);
$aux++;
}
}
echo "\n\nArquivo de saída gerado!\n\n";
}
function assembler($arrayF, $typeR, $typeI, $typeJ, $registers, $argv)
{
$fileOut = (!empty($argv[2])) ? $argv[2] : "memoria.mif";
$file = fopen($fileOut, "w");
if (!$file) {
echo "\n# Falha ao tentar criar o arquivo quickpatricia-eu.mif!\n";
}
$instEnd = array();
$arrayF = format($arrayF);
$arrayLen = count($arrayF);
$output = "";
for ($i = 0; $i < $arrayLen; $i++) {
$sub = array(",", "$");
$address = ($i * 4);
$labelF = strrpos($arrayF[$i], ":");
$instr = $labelF ? substr($arrayF[$i], $labelF + 1) : $arrayF[$i];
$instr = explode(" ", trim($instr));
if (array_key_exists($instr[0], $typeR)) {
if ($instr[0] == "sll" || $instr[0] == "srl") {
$rd = trim(str_replace($sub, "", $instr[1]));
$rt = trim(str_replace($sub, "", $instr[2]));
$shamt = $instr[3] ? trim(str_replace($sub, "", $instr[3])) : 0;
$rs = "0";
} elseif ($instr[0] == "jr") {
$rs = trim(str_replace($sub, "", $instr[1]));
$rd = "0";
$rt = "0";
$shamt = "0";
} else {
$rd = trim(str_replace($sub, "", $instr[1]));
$rs = trim(str_replace($sub, "", $instr[2]));
$rt = $instr[3] ? trim(str_replace($sub, "", $instr[3])) : 0;
$shamt = "0";
}
$return = typeR($address, $rs, $rt, $rd, $registers, $shamt, $typeR, $instr[0]);
if ($return == -1) {
$output .= "\nA linha " . ($i + 1) . " possui algum erro.";
} else {
$instEnd[$i] = $return;
}
} elseif (array_key_exists($instr[0], $typeI)) {
if (strpos($instr[2], "(")) {
$begin = strpos($instr[2], "(");
$end = strpos($instr[2], ")");
$instr[3] = trim(substr($instr[2], $begin + 1, $end - strlen($instr[2])));
$instr[2] = trim(substr($instr[2], 0, $begin));
$imm = trim(str_replace($sub, "", $instr[2]));
$rs = trim(str_replace($sub, "", $instr[3]));
$rt = trim(str_replace($sub, "", $instr[1]));
} elseif ($instr[0] == "lui") {
$rs = "0";
$rt = trim(str_replace($sub, "", $instr[1]));
$imm = trim(str_replace($sub, "", $instr[2]));
} elseif ($instr[0] == "bne" || $instr[0] == "beq") {
$rs = trim(str_replace($sub, "", $instr[1]));
$rt = trim(str_replace($sub, "", $instr[2]));
$imm = trim(str_replace($sub, "", $instr[3]));
} else {
$rs = trim(str_replace($sub, "", $instr[2]));
$imm = trim(str_replace($sub, "", $instr[3]));
$rt = trim(str_replace($sub, "", $instr[1]));
}
$labelDef = (ctype_digit($imm) || $imm < 0) ? -1 : findLabel($arrayF, $arrayLen, $imm);
if ($labelDef == -1) {
$instEnd[$i] = typeI($address, $rs, $rt, $imm, $registers, $typeI, $instr[0]);
} else {
$instEnd[$i] = typeI($address, $rs, $rt, $labelDef, $registers, $typeI, $instr[0]);
}
} elseif (array_key_exists($instr[0], $typeJ)) {
$addr = trim(str_replace($sub, "", $instr[1]));
$labelDef = findLabel($arrayF, $arrayLen, $addr);
if ($labelDef == -1) {
$instEnd[$i] = typeJ($address, $addr, $typeJ, $instr[0]);
} else {
$instEnd[$i] = typeJ($address, $labelDef, $typeJ, $instr[0]);
}
} elseif (!$labelF) {
$output .= "\nA linha " . ($i + 1) . " não possui uma instrução válida!\n";
}
}
if ($output) {
echo "\n\n\nErros:" . $output . "\n";
}else {
fileWrite($file, $instEnd, $arrayLen);
}
fclose($file);
}
$arrayF = ($argv[1]) ? file($argv[1]) : 0;
if (!$arrayF) {
echo "\n\n# Arquivo de dados não foi especificado pelo usuário!\n\n";
exit(1);
}
assembler($arrayF, $typeR, $typeI, $typeJ, $registers, $argv);
甚至不会呈现{{ form }}
标签或任何类型的Submit元素。它旨在成为呈现非常非常基本的字段集的非常非常基础的方法,而admin是专门设计成(合理地)强大而灵活的方法来将UI置于模型之前。
您当然可以自行构建该功能,或者找到一个可重复使用的应用程序来执行相同的功能,但是Django没有提供可自动生成该功能的功能。
答案 1 :(得分:0)
没有类似的内置功能。</ p>
但是,您可以自己轻松构建它。 您将必须在模板中添加一个链接(或HTML表单),该链接指向与您为创建给定模型而实现的视图相对应的URL。
以下是一个非常抽象的示例。
在您的模板中:
<form>
{{csrf_token}}
{{ form }}
<a href="/models/createA"> Create model A if you want </a>
<input type="submit" value="Submit">
<\form>
在您的urls.py
中 url(r'^models/createA/$', views.CreateModelAView.as_view(), name="optional")
在您的views.py
createModelAView(CreateView):
model = ModelA
form_class = ModelAForm
然后,您需要创建一个名为ModelAForm
的表单。
另外,如果您是Django的新手,建议您从功能视图开始。它的编码更多,但是您可以更好地了解正在发生的事情