首页 \ 问答 \ 如何在Django中立即向ManyToMany关系添加多个对象?(How to add multiple objects to ManyToMany relationship at once in Django ?)

如何在Django中立即向ManyToMany关系添加多个对象?(How to add multiple objects to ManyToMany relationship at once in Django ?)

基于Django文档,我应该能够一次传递多个对象以添加到manytomany关系,但是我得到一个

* TypeError:不可分类型:'list'

当我尝试通过一个列在列表中的django queryset。 传递Queryset或ValuesListQueryset似乎也失败了。 有没有比使用for循环更好的方法?


Based on the Django doc, I should be able to pass multiple objects at once to be added to a manytomany relationship but I get a

* TypeError: unhashable type: 'list'

when I try to pass a django queryset casted in a list. Passing a Queryset or a ValuesListQueryset seems to fail also. Is there a better way than use a for loop ?


原文:https://stackoverflow.com/questions/4959499
更新时间:2022-09-02 10:09

最满意答案

首先,语法用于从表达式构造解析树。 所以如果你已经有一个解析树,你不需要语法。

根据您的解析器的工作量,通过解析表达式形成的结果树可能已经是抽象语法树。 或者它可以是一个简单的解析树,需要二次构建ast。

要从语法和表达式构造解析树,您首先必须将您的语法转换为工作代码。 通常,您将工作分解为将代表表达式的输入流分解为令牌列表的令牌程序,以及一个解析器,它从令牌中获取令牌列表并构建解析树\ ast。

所以表达式1 + 2*(3+4)可能被分成一个这样的令牌列表:

1 - int
+ - add_operator
2 - int
* - mul_operator
( - lparen
3 - int
+ - add_operator
4 - int
) - rparen

第一列是实际的文本值。 第二个代表令牌类型。 这些令牌被输入到从你的语法构建的解析器中,并识别令牌并构建解析树。

那么,如何编写词法分析器和实际的解析器呢? 你可以手工滚动自己。 或者更常见的是使用像Coc或antlr或lex / yacc这样的解析器生成器。 这些工具对您的语法进行了描述,并生成了一个tokenzier和解析器的代码。 (代码生成器存在于大多数流行语言和一些不受欢迎的语言中。)

构建解析器的方式在很大程度上取决于您使用的语言。 在Haskell中写一个解析器的方法与你在C中的说法完全不同

  • 这是一个教程,向您展示如何构建自己的递归下降解析器 。

  • Coco是用于各种语言的解析器生成器,它还附带了有关如何开始的文档。

  • 如果你的Python是你的东西,那么你可能会想到 Python。


Well, first off, the grammar is used to construct a parse tree from an expression. So if you already have a parse tree, you don't need the grammar.

Depending on how much work your parser does, the resulting tree that is formed from parsing an expression could already be an abstract syntax tree. Or it could be a simple parse tree which requires a second pass to construct the ast.

To construct the parse tree from a grammar and an expression, you would first have to convert your grammar into working code. Typically, you would split the work into a tokenizer which splits the input stream representing the expression into a list of tokens, and a parser which takes the list of tokens and constructs a parse tree\ast from it.

So the expression 1 + 2*(3+4) might be split into a list of tokens like this:

1 - int
+ - add_operator
2 - int
* - mul_operator
( - lparen
3 - int
+ - add_operator
4 - int
) - rparen

The first column is the actual text value. The second represents the token type. These tokens are fed into the parser, which is built from your grammar and recognizes the tokens and builds the parse tree.

So, how does one write the lexical tokenizer and the actual parser? You could roll your own by hand. Or, more commonly, use a parser generator like coco or antlr or lex/yacc. These tools take a description of your grammar and generate the code for a tokenzier and parser. (Code generators exist for most popular languages and some unpopular ones as well.)

How you construct your parser depends heavily on what language you use. How you would write a parser in Haskell is entirely different from how you would in, say, C.

相关问答

更多
  • 首先,您需要了解解析是什么,以及什么抽象语法树。 为此,您可以首先查看抽象语法树上的维基百科 。 你真的需要花一些时间在编译器的教科书上来理解抽象语法树是如何与解析相关的,并且可以在解析时构建; 经典的参考文献是Aho / Ullman / Sethi的“编译器”一书(很容易在网上找到)。 你可能会找到答案是否有任何“有趣”的方式来学习语言,语法,解析和编译器? 启发。 一旦你明白如何为一个简单的语法构建一个AST,你就可以把注意力转向像C#这样的东西。 这里的问题很大, 玩20种语法规则的玩具语言是一回事 ...
  • 你的具体问题的答案是这样的:prev链接意味着,当你的代码有一个指向这些节点之一的指针时,它可以跟随链中前一个链接的链接。 符号表可能有这样一个列表的一个原因是处理嵌套范围: { int x; { int x; } } 但是,为什么符号节点可能需要排列在列表中,还有许多其他原因。 编译器需要访问所有节点的任何原因都是一个原因。 The answer to your concrete question is this: the prev link means that, when your c ...
  • 也许你在这个食谱中找到了一些灵感: http://code.activestate.com/recipes/533146-ast-pretty-printer/ 一个输出Python AST的人类可读版本的函数。 或者使用compiler结合inspect (当然,仍然使用源代码): >>> import compiler, inspect >>> import re # for testing >>> compiler.parse(inspect.getsource(re)) Module('Suppo ...
  • 首先,语法用于从表达式构造解析树。 所以如果你已经有一个解析树,你不需要语法。 根据您的解析器的工作量,通过解析表达式形成的结果树可能已经是抽象语法树。 或者它可以是一个简单的解析树,需要二次构建ast。 要从语法和表达式构造解析树,您首先必须将您的语法转换为工作代码。 通常,您将工作分解为将代表表达式的输入流分解为令牌列表的令牌程序,以及一个解析器,它从令牌中获取令牌列表并构建解析树\ ast。 所以表达式1 + 2*(3+4)可能被分成一个这样的令牌列表: 1 - int + - add_operato ...
  • 在AST(而不是CFG)上最容易实现的优化是tail-call优化:如果你看到一个表格的子树: RETURN CALL f ARGS x, y, ... 您可以通过跳转到f来替换它。 如果f(a, b)是尾部呼叫出现的函数,则替换操作很简单: a = x; b = y JUMP to root of tree 我发现把这个跳转表示为一个特殊的“重新启动”语句是最容易的,AST-> CFG翻译把它看作是回到第一个节点的边缘。 跳到其他函数有点棘手,因为你不能只设置局部变量,你需要事 ...
  • 您当前的解析器只会为您提供已解析的令牌的平面列表,因为这是pyparsing中的默认值。 目的是这样,无论你如何构建解析器,无论是小块还是将它们放在一起,或者只是在一个巨大的声明中,你从解析中获得的标记都是相同的结构化(或非结构化)。 为了获得类似于AST的东西,你需要使用pyparsing的Group类定义你想要的结构(我建议也使用结果名称)。 例如,如果您将语句更改为: statement = Group(ident("lhs") + '=' + Group(expr)("rhs")) 然后你的输出将 ...
  • 使用ANTLR是开始这样做的好方法。 但是:首先构建一些简单的东西,比如表达式解析器。 这将确保您了解基础知识。 您可能会在ANTLR中找到几十个这样的示例,我怀疑即使在堆栈溢出也是如此。 获得表达式语法和AST后, 再考虑XPath。 您会发现这很复杂,主要是因为XPath的定义是由一个委员会创建的,建立在另一个委员会的巨型XML构建之上。 因此,您的问题将有点了解如何使用解析器构建AST,以及阅读这两个委员会编写的用于定义XPath的所有内容的LOT。 Using ANTLR is a fine way ...
  • 我会分三步来解决问题: 按名称导入模块。 这应该相对容易使用importlib.import_module ,但如果需要的话,您可以使用内建的__import__来保留自己的版本。 获取模块的源代码。 使用inspect.getsource可能是最简单的方法(但您也可以尝试open(the_module.__file__).read()并且它可能会工作)。 将源解析为AST。 ast.parse应该很容易。 即使对于这一步,库也不是必需的,因为只要您传递适当的标志(您的系统上ast.PyCF_ONLY_AS ...
  • Ast.expr类型定义看起来是错误的:它不代表抽象语法树,而仅代表原子表达式的语法。 这是因为类型根本不是递归的,所以很难称之为树。 相比之下, Sexp.expr是一种递归类型。 我的猜测是你忘记了类型定义中的一个案例,例如: type expr = | Expr_unit | Expr_int of int | Expr_sym of sym | Expr_call of expr list 一旦完成,这两种类型实际上是相同的,因此转换变得简单。 The As ...
  • 节点需要包含“抽象”语法类别(通常接近许多规则的LHS)。 有关AST与CST的讨论,请参阅https://stackoverflow.com/a/1916687/120163 ,以及为什么您可能希望节点包含具体语法类别(例如,确切的LHS规则或叶子的具体名称) )。 该链接讨论了何时应在AST中保留终端。 如果它们表示语法不记录的方式(例如,标识符名称,数字和字符串文字常量等),它们也应该包含值 。请参阅https://stackoverflow.com/a/6320259/120163了解原因你应该在l ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)