using prefetch_related to get only a particular entry of a relation - django

Assume the following models
Class A(): pass
Class B():
i = integer()
aa = foreignkey('A', related_name = 'fka')
For simplicity assume the below entries,
A() - a1 -> pk = 1
B() - b1 -> i = 1, aa = a1
B() - b2 -> i = 2, aa = a1
B() - b3 -> i = 3, aa = a1
B() - b4 -> i = 4, aa = a1
B() - b5 -> i = 5, aa = a1
I know,
foo = A.objects.get(pk = 1).prefetch_related('fka')
will give me the entries b1, b2, b3, b4 and b5.
But what I want to know is, is it possible to alter this query in anyway to get only a particular entry of B() associated with A().
Suppose I want to prefetch only entry of 'B', with 'i' as 2 (get b2, and skip b1, b3, b4 and b5).
If it is not possible to do it using the prefetch then, what is the best way to do it ?
Note: get(pk = 1) is used to keep the simplicity for explanation, but at that place usually there will be filter(**conditions).

Is it necessary to do the query on the A objects rather than the B objects? If you reverse what you do the additional lookup on, you could use select_related():
foo = B.objects.select_related('aa').get(i=2)
This has the advantage of reducing your number of database hits. Because prefetch_related() always does its own database lookup, your proposed scenario of using prefetch_related() isn't any more efficient, as written, than simply doing:
a = A.objects.get(pk=1)
b = a.fka.get(i=2)
That's not to say it wouldn't be more efficient in scenarios with more complex filtering, but select_related() uses a SQL join and therefore only hits the database once. That said, if you absolutely must lookup the A objects first, the best answer would be: upgrade to Django 1.7. The new Prefetch command in Django 1.7 allows for more complex operations on prefetch_related(), including filtering, by allowing you to specify custom querysets:
qs = B.objects.filter(i=2)
foo = A.objects.get(pk = 1).prefetch_related(Prefetch('fka',qs))

Related

In the graph theory, is a strongly connected component SCC form a DAG?

I was trying to solve a problem to design an algorithm to determine whether a direct graph is semi-connected. Someone says it can be done by using topological sort every SCC in the graph. And SCC is guaranteed to be DAG. However, I think SCC graph must be a circle, why it is a DAG since DAG mean no circle.
You misunderstood the argument.
Suppose you have a graph that has points
A1 <--> A2 <--> A3 --> B1 <--> B2 --> C1 <--> C2
and A1 A2 A3, B1 B2, C1, C2 are SCC.
Then you treat A1 A2 A3 as a single point A. Any node connecting to one of A1 A2 A3 is treated as connecting to A, Any node connected from one of A1 A2 A3 is treated as connected from A. Same for merging points to B, C
So it became A --> B --> C. It is guaranteed that this is a DAG.

Need Time efficient data structure to store computed value using values from different maps that have intersecting key type but same value type

I have a problem
I have 4 classes
Need to maintain 3 maps
class A
{
};
class B
{
};
class D
{
int d1;
int d2;
};
std::unordered_map<A, D> m1;
std::unordered_map<B, D> m2;
std::unordered_map<std::pair<A, B>, D> m3;
Note: These maps are not updated frequently.
Problem
1. For object of A: a, object of B: b, and object of pair of object of a and object of b,
2. Need to get an object of D: d such that
d.d1 = std::min( m1[a].d1, m2[b].d1, m3[std::pair(a,b)].d1 );
d.d2 = std::min( m1[a].d2, m2[b].d2, m3[std::pair(a,b)].d2 );
Note: The values may not exist in which case they will be skipped, eg: m1[a] does not exist, for a pair(a, b), than min( m2[b], m3[std::pair(a,b)] ) will be used
The simple solution is
1. find
m1[a], m2[b], m3[pair(a, b)]
three look-ups
2. perform minimum on d1 and d2
3. Get d
I can not save every b for every a, as the number of possible keys of A and B is huge
Question
1. I need to store this info in such a structure such that for a pair of a and b, I can get d, in less than three look-ups, I need to reduce the time for look-ups to the minimum while the space may be increased.
2. Also, the time of updating that structure can be more than updating the three maps.
3. If anyone knows any such data structure, please provide the name or the link or some explanation. Thanks.
Regards

Pig: Joining without nested renaming

I have two datasets
A(af1, af2, af3)
B(bf1, bf2, bf3)
When I join them in Pig as
C = Join A by af1, B by bf1
And subsequently store as a JSON (after removing the join-predicate column)
store C into 'output.son' using JsonStorage();
I see a JSON schema as
{"A::af1":val, "A::af2":val, ...., "B::bf2":val, ...}
Is there a way I can strip off the unnecessary (as I am taking care of the ambiguity already) nesting-like naming resulting from the join?
Thanks in advance
We have to iterate over relation/alias C and generate the required fields and then store the new alias, lets say new alias is D.
D = FOREACH C GENERATE A::af1 AS af1, A::af2 AS af2, A::af3 AS af3, B::bf2 AS bf2, B::bf3 AS bf3;
STORE D INTO 'output.son' USING JsonStorage();
Update :
If there are 100 of unique field names in alias A, likewise in B, then after join we can use .. operator and select the required columns. We can even access the required fields using position notation also ($0..$99,$101..$200)
C = JOIN A BY af1, B BY bf1;
D = FOREACH C GENERATE af1..af100,bf2..bf100;
STORE D INTO 'output.son' USING JsonStorage();

CODE1 at SPOJ - cannot solve it

I am trying to solve the problem Secret Code on SPOJ, and it's obviously a math problem.
The full problem
For those who are lazy to go and read, it's like this:
a0, a1, a2, ..., an - sequence of N numbers
B - a Complex Number (has both real and imaginary components)
X = a0 + a1*B + a2*(B^2) + a3*(B^3) + ... + an*(B^n)
So if you are given B and X, you should find a0, a1, ..an.
I don't know how or where to start, because not even N is known, just X and B.
The problem is not as easy as expressing a number in a base B, because B is a complex number.
How can it be solved?
The key is that a0 .. an are not arbitrary numbers, they're integers (otherwise, this wouldn't be possible in general). You're given the number X , and are asked to express it in base B. Why don't you start by working a few examples for a specific value of B?
If I asked you to write 17 in base 2, would you be able to do that? Can you find a way to generalize the algorithm you use to bases other than 2?

Filter Many to Many relation in Django

I have this structure of model objects:
Class A:
b = models.ManyToManyField("B")
Class B:
c = models.ForeignKey("C")
d = models.ForeignKey("D")
Class C:
d = models.ForeignKey("D")
This is the query I'm trying to get:
I want to get all the B objects of object A, then in each B object to perform a comparison between the D object and the c.d object.
I know that simply move on the B collection with for loop and make this comparison.
But I dived on the ManyToMany relation, then I noticed I can do the following:
bObjects = A.objects.all().b
q = bObjects.filter(c__d=None)
This is working, it gives me all the c objects with None d field. But when I try the following :
q = bObjects.filter(c__d=d)
It gives me d not defined, but d is an object like c in the object B.
What can be the problem? I'll be happy if you suggest further way to do this task.
I generally I'm trying to write my query in a single operation with many to many sub objects and not using loops.
q = bObjects.filter(c_d=d) //Give me d not defined. but d is an object like c in the object B.
Try this:
from django.db.models import F
q = bObjects.filter(c__d=F('d'))
As for the question from your comment below you can have 1 sql query instead of 100 in those ways:
1) if you can express your selection of A objects in terms of a query (for example a.price<10 and a.weight>20) use this:
B.objects.filter(a__price__lt=10, a__weight__gt=20, c__d=F('d'))
or this:
B.objects.filter(a__in=A.objects.filter(price__lt=10, weight__gt=20), c_d=F('d'))
2) if you just have a python list of A objects, use this:
B.objects.filter(a__pk__in=[a.pk for a in your_a_list], c__d=F('d'))