如何在Django中进行三级查询?

时间:2017-10-25 13:04:39

标签: python django

我有三个表,名称++-- Copyright © 2017 Martin Ueding <dev@martin-ueding.de> -- Ising model with the Metropolis algorithm. Random choice of lattice site for -- a spin flip. import qualified Data.Text import System.Random type Spin = Bool type Lattice = [Spin] -- Lattice extent is fixed to a square. extent = 30 volume = extent * extent temperature :: Double temperature = 0.0 -- Converts a `Spin` into `+1` or `-1`. spin :: Spin -> Int spin True = 1 spin False = (-1) -- Wrap a coordinate for periodic boundary conditions. wrap :: Int -> Int wrap = flip mod $ extent -- Converts an unbounded (x,y) index into a linearized index with periodic -- boundary conditions. index :: Int -> Int -> Int index x y = wrap x + wrap y * extent -- Retrieve a single element from the lattice, automatically performing -- periodic boundary conditions. get :: Lattice -> Int -> Int -> Spin get l x y = l !! index x y -- Computes the sum of neighboring spings. neighborSum :: Lattice -> Int -> Int -> Int neighborSum l x y = sum $ map spin $ map retriever neighbors where retriever = \(x, y) -> get l x y neighbors = [(x+1,y), (x-1,y), (x,y+1), (x,y-1)] -- Computes the energy difference at a certain lattice site if it would be -- flipped. energy :: Lattice -> Int -> Int -> Int energy l x y = 2 * neighborSum l x y * (spin (get l x y)) -- Converts a full lattice into a textual representation. latticeToString l = unlines lines where spinToChar :: Spin -> String spinToChar True = "#" spinToChar False = "." line :: String line = concat $ map spinToChar l lines :: [String] lines = map Data.Text.unpack $ Data.Text.chunksOf extent $ Data.Text.pack line -- Populates a lattice given a random seed. initLattice :: Int -> (Lattice,StdGen) initLattice s = (l,rng) where rng = mkStdGen s allRandom :: Lattice allRandom = randoms rng l = take volume allRandom -- Performs a single Metropolis update at the given lattice site. update (l,rng) x y | doUpdate = (l',rng') | otherwise = (l,rng') where shift = energy l x y r :: Double (r,rng') = random rng doUpdate :: Bool doUpdate = (shift < 0) || (exp (- fromIntegral shift / temperature) > r) i = index x y (a,b) = splitAt i l l' = a ++ [not $ head b] ++ tail b -- A full sweep through the lattice. doSweep (l,rng) = doSweep' (l,rng) (extent * extent) -- Implementation that does the needed number of sweeps at a random lattice -- site. doSweep' (l,rng) 0 = (l,rng) doSweep' (l,rng) i = doSweep' (update (l,rng'') x y) (i - 1) where x :: Int (x,rng') = random rng y :: Int (y,rng'') = random rng' -- Creates an IO action that prints the lattice to the screen. printLattice :: (Lattice,StdGen) -> IO () printLattice (l,rng) = do putStrLn "" putStr $ latticeToString l dummy :: (Lattice,StdGen) -> IO () dummy (l,rng) = do putStr "." -- Creates a random lattice and performs five sweeps. main = do let lrngs = iterate doSweep $ initLattice 2 mapM_ dummy $ take 1000 lrngs A

B

我想获得波纹管数据:

C

你看,有三级数据,如果只有表A和B,我可以多表连接查询:

class A(models.Model):
    name = models.CharField(max_length=12)

class B(models.Model):
    name = models.CharField(max_length=12)
    a = models.ForeignKey(to=A)

class C(models.Model):
    name = models.CharField(max_length=12)
    email = models.EmailField()
    b = models.ForeignKey(to=B)

但是现在有三个表,您看到表[ {"name":"a1", "data":[{ "name":"b1", "data":[ {"name":"c1", "data":{"c1_name":"c1_name", "c1_id":"c1_id"} }, {"name":"c2", "data":{"c2_name":"c2_name", "c2_id":"c2_id"} } ] }, { "name":"b2", "data":[ {"name":"c1", "data":{"c1_name":"c1_name", "c1_id":"c1_id"} }, {"name":"c2", "data":{"c2_name":"c2_name", "c2_id":"c2_id"} } ] } ] } ] 的电子邮件不包含在查询数据中。 如何在我的场景中实现这个要求?

1 个答案:

答案 0 :(得分:0)

正如@Brian H.在评论中指出的那样,你可以用[{1}}链接外键。

在您的情况下,您可以使用

获取数据
__

然后,您可以循环访问数据并以您需要的格式构建列表。