Django Rest Serializer:反向关系(Django Rest Serializer: Reverse relationships)
免责声明:我正在学习django,因为我将它应用于我继承的数据库/ PHP应用程序。 数据库有点混乱,没有外键约束,命名也不一致。 我不想触摸或重做数据库上的任何内容,因为我根本不想使用遗留应用程序。
Stack:Python 2.7。 Django 1.5,Django Rest Framework
问题是我有一个关系,其中有一个具有多个代码的Idea。 代码表有想法的外键(teaser_id),所以我们有类似的东西
**Tickers** id teaser_id 1 1 2 1 3 1 4 2 4 2 **Ideas** id 1 2
我让django从数据库生成模型,但没有FK约束,它没有正确生成所有关系。 所以这里有模型配置:
class Tickers(models.Model): id = models.IntegerField(primary_key=True) # I changed to this teaser_id = models.ForeignKey(Idea) # From # teaser_id = models.IntegerField(null=True, blank=True) ticker = models.CharField(max_length=135L, blank=True) date_added = models.CharField(max_length=135L, blank=True) class Meta: db_table = 'teaser_tickers' class Idea(models.Model): id = models.IntegerField(primary_key=True) industry_id = models.IntegerField() post_type = models.CharField(max_length=45L) class Meta: db_table = 'idea'
这是我的序列化器
class TickerSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = myModels.Tickers fields = ( 'id' ,'teaser_id' ,'ticker' ) class IdeaSerializer(serializers.HyperlinkedModelSerializer): user = UserSerializer(many=False, read_only=True) ticker = TickerSerializer(many=True, read_only=True, ) teaser = myFields.TeaserField(teaser_length=200, original_field='content') class Meta: model = myModels.Idea fields = ( 'id' , 'title' , 'date_added' , 'user' , 'teaser' , 'ticker' )
我希望Ideas Resource将代码作为子节点集返回。
REST请求用于Idea,其中代码是子元素。 所以我得到一个例外,即故障定义没有在想法中定义。 很好 - 但我只是猜测如何在这一点上设置它 - 我通过文档和源代码进行了抨击 - 但希望有人可以帮助我。
谢谢
Disclaimer: I am learning django as I apply it to a database / PHP app i inherited. The database is a bit of a mess, with no foreign key constrains, and inconsistent naming. I do not want to touch or redo anything on the database because I don't want to mes with the legacy application at all.
Stack: Python 2.7. Django 1.5, Django Rest Framework
The problem is that I have a relationship where there is an Idea that has multiple Tickers. The tickers table has the foreign key to the ideas (teaser_id) so that we have something like
**Tickers** id teaser_id 1 1 2 1 3 1 4 2 4 2 **Ideas** id 1 2
I had django generate the model from the database, but without the FK Constraints it didn't generate all the relationships properly. So here is haw the models are configured:
class Tickers(models.Model): id = models.IntegerField(primary_key=True) # I changed to this teaser_id = models.ForeignKey(Idea) # From # teaser_id = models.IntegerField(null=True, blank=True) ticker = models.CharField(max_length=135L, blank=True) date_added = models.CharField(max_length=135L, blank=True) class Meta: db_table = 'teaser_tickers' class Idea(models.Model): id = models.IntegerField(primary_key=True) industry_id = models.IntegerField() post_type = models.CharField(max_length=45L) class Meta: db_table = 'idea'
Here are my serializers
class TickerSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = myModels.Tickers fields = ( 'id' ,'teaser_id' ,'ticker' ) class IdeaSerializer(serializers.HyperlinkedModelSerializer): user = UserSerializer(many=False, read_only=True) ticker = TickerSerializer(many=True, read_only=True, ) teaser = myFields.TeaserField(teaser_length=200, original_field='content') class Meta: model = myModels.Idea fields = ( 'id' , 'title' , 'date_added' , 'user' , 'teaser' , 'ticker' )
I want the Ideas Resource to return the tickers as a child node set.
The REST request is for the Idea where the tickers is a child element. So I am getting an exception that ticker isn't defined in idea. Fine get that - but I am just guessing on how to set this up at this point - I am sludging through documentation and source - but was hoping someone could help me out.
THank you
原文:https://stackoverflow.com/questions/19326327
最满意答案
这是原始
Expr
类型的变体,它添加了变量(V
)和let绑定(Let
)。data Expr = C Float | V String | Let [(String, Expr)] Expr | Expr :+ Expr | Expr :- Expr | Expr :* Expr | Expr :/ Expr
为了帮助编写
evaluate
,您可能希望从一个执行变量替换的函数开始:data Expr = C Float | V String | Let [(String, Expr)] Expr | Expr :+ Expr | Expr :- Expr | Expr :* Expr | Expr :/ Expr deriving Show -- | @sub var value e@ replaces variables named @var@ with the value @value@ -- wherever anywhere that variable occurs in expression @e@. sub :: String -> Expr -> Expr -> Expr -- "let x = y in x" = y sub v1 value (V v2) | v1 == v2 = value -- "let x = y in z" = z sub _ _ e@(V _) = e -- Constants are unaffected sub _ _ c@(C _) = c -- For operators, apply @sub a b@ recursively to the operands. sub a b (e1 :+ e2) = (sub a b e1) :+ (sub a b e2) sub a b (e1 :- e2) = (sub a b e1) :- (sub a b e2) sub a b (e1 :* e2) = (sub a b e1) :* (sub a b e2) sub a b (e1 :/ e2) = (sub a b e1) :/ (sub a b e2) -- The variable is shadowed by a let binding, so only substitute -- into the bindings, and leave the body expression unmodified. sub a b (Let bindings e) | bindingsContains a bindings = Let (subIntoBindings a b bindings) e -- Apply @sub a b@ recursively to the body of the let expression. sub a b (Let bindings body) = Let (subIntoBindings a b bindings) (sub a b body) bindingsContains :: String -> [(String, Expr)] -> Bool bindingsContains x bindings = Data.Maybe.isJust $ Data.List.find ((== x) . fst) bindings subIntoBindings :: String -> Expr -> [(a, Expr)] -> [(a, Expr)] subIntoBindings a b bindings = (fmap . fmap) (sub a b) bindings
Here's a variant of the original
Expr
type that adds variables (V
) and let bindings (Let
).data Expr = C Float | V String | Let [(String, Expr)] Expr | Expr :+ Expr | Expr :- Expr | Expr :* Expr | Expr :/ Expr
To help write
evaluate
, you may want to start with a function that does variable substitution:data Expr = C Float | V String | Let [(String, Expr)] Expr | Expr :+ Expr | Expr :- Expr | Expr :* Expr | Expr :/ Expr deriving Show -- | @sub var value e@ replaces variables named @var@ with the value @value@ -- wherever anywhere that variable occurs in expression @e@. sub :: String -> Expr -> Expr -> Expr -- "let x = y in x" = y sub v1 value (V v2) | v1 == v2 = value -- "let x = y in z" = z sub _ _ e@(V _) = e -- Constants are unaffected sub _ _ c@(C _) = c -- For operators, apply @sub a b@ recursively to the operands. sub a b (e1 :+ e2) = (sub a b e1) :+ (sub a b e2) sub a b (e1 :- e2) = (sub a b e1) :- (sub a b e2) sub a b (e1 :* e2) = (sub a b e1) :* (sub a b e2) sub a b (e1 :/ e2) = (sub a b e1) :/ (sub a b e2) -- The variable is shadowed by a let binding, so only substitute -- into the bindings, and leave the body expression unmodified. sub a b (Let bindings e) | bindingsContains a bindings = Let (subIntoBindings a b bindings) e -- Apply @sub a b@ recursively to the body of the let expression. sub a b (Let bindings body) = Let (subIntoBindings a b bindings) (sub a b body) bindingsContains :: String -> [(String, Expr)] -> Bool bindingsContains x bindings = Data.Maybe.isJust $ Data.List.find ((== x) . fst) bindings subIntoBindings :: String -> Expr -> [(a, Expr)] -> [(a, Expr)] subIntoBindings a b bindings = (fmap . fmap) (sub a b) bindings
相关问答
更多-
Haskell LLVM绑定 - 使用add指令引起的类型错误(Haskell LLVM bindings - Type error arising from use of add instruction)[2022-05-29]
看看源代码,我们有iadd :: (IsInteger c, ABinOp ab (vc)) => a -> b -> CodeGenFunction r (vc) 。 然后, iadd的前两个参数必须是iadd成员 - 但你已经知道了这一点,GHC告诉你。 我怀疑,我认为(v0 c0)不是问题。 从源头看, ABinOp定义为class ABinOp abc | ab -> c where class ABinOp abc | ab -> c where 。 最后一个类型参数c由给定任意两个输入参数的类型 ... -
它不是Prelude的Enum实例(请参阅文件顶部附近的import Prelude hiding (Enum(..)) )。 它是Graphics.UI.SDL.Utilities的Enum实例,它带有第二个参数: class Enum a b | a -> b where succ :: a -> a pred :: a -> a toEnum :: b -> a fromEnum :: a -> b enumFromTo :: a -> a -> [a] 与前奏中的定义相比: c ...
-
如果您想通过ghci使变量类型脱机,则必须输入 :t 表达式 如果你想在ghci中创建变量,你必须使用let而不使用'in'(如monad的表示法,我不知道你是否已经看过它们): let var = expr 如果你自己检查一下,你应该能够更容易地为你的考试记住它。 (祝你好运;)) If you want to get types of variables offline with ghci you have to type :t expression if you want to create vari ...
-
Haskell中的简单算术(Simple arithmetic in Haskell)[2024-04-18]
这里的问题是(从错误消息中推导出) priceOf返回一个Int而0.95 * 8 * 2具有多态类型Fractional a => a ,并且shouldBe强制这些类型相同。 但是Int不是Fractional类型,所以你会得到“没有Fractional Int实例”。 如何解决这个问题: 使priceOf返回Fractional类型,例如Double或 根据您的要求,使用round , floor或ceiling之一将0.95 * 8 * 2转换为Int 。 The problem here is t ... -
我真的不明白你要做什么,但我会说这个。 类型主要用于对值进行分类。 构建大量的类型级别信息,使用Proxy在值级别擦除它,然后尝试使用类来恢复它以在类型上进行模式匹配会导致复杂的代码(如您所见)并且不会在安全性或简洁性方面真的给你带来任何东西。 把事情简单化。 我的建议是更仔细地考虑您的API客户提前知道哪些信息 - 这是类型级的东西 - 以及客户想要动态构建的内容。 使用类型级别的东西来对值级别的东西进行分类。 在这种情况下,您的用户将提前知道他们的架构 - 模型的各个维度 - 但他们通常不会知道他们将查 ...
-
Haskell中的代数数据类型(Algebraic Data Type in Haskell)[2023-05-29]
你可以使用,例如, data BinOp = Add | Mul data UnaryOp = Sin | Cos data Expr = Num Double | Binary BinOp Expr Expr | Unary UnaryOp Expr YMMV关于这是否更简单。 You can use, e.g., data BinOp = Add | Mul data UnaryOp = Sin | Cos data Expr = Num Double | Binary Bin ... -
几乎就是这样,但有一些轻微的语法调整,所以你说的是GHC的语言。 data Expr = Numb Int | Add Expr Expr | Diff Expr Expr | Mult Expr Expr eval :: Expr -> Int eval (Numb x) = x eval (Add e e') = eval e + eval e' eval (Diff e e') = eval e - eval e' eval (Mult e e') = eval e * eval e' 在ghci ...
-
我不是该库的专家,但似乎是为了允许用户定义的装饰而设计的。 这是因为所有主要数据类型都通过NodeInfo进行参数化, NodeInfo是标准注释(仅携带位置和名称信息)。 例如图书馆提供 type CTranslUnit = CTranslationUnit NodeInfo 它允许您定义 type MyTransUnit = CTranslationUnit MyNodeInfo data MyNodeInfo = MNI NodeInfo AdditionalStuffHere 所以按照你的意愿装 ...
-
使用haskell中的let绑定的算术表达式的数据类型(Data type for arithmetic expressions with let bindings in haskell)[2023-05-26]
这是原始Expr类型的变体,它添加了变量( V )和let绑定( Let )。 data Expr = C Float | V String | Let [(String, Expr)] Expr | Expr :+ Expr | Expr :- Expr | Expr :* Expr | Expr :/ Expr 为了帮助编写evaluate ,您可能希望从一个执行变量替换的函数开始: data Expr = C Float | V Strin ... -
如果你还派生了一个Enum实例,你可以使用fromEnum :: Enum a => a -> Int和toEnum :: Enum a => Int -> a在你的数据类型和整数之间进行转换,这些数据类型和整数对应于它们在数据定义中的位置。 由于您在第0个位置上有Zero ,所以这就是您想要进入整数和整数的正确值。 但你还应该考虑: addNumber Three Two的结果是什么? 这里没有正确的价值,因为你对数字的定义不会达到五。 无论您设置了哪个上限,都会有一个“部分”函数,该函数在其域中的某些值 ...