So here is the scenario. I get user_id and do a filter named generated. I want to show all the values of class C by order of check.
Here is my models.py
class A(models.Model):
tob = models.ForeignKey(B)
def something(self, user):
return A.objects.filter(to_b=user)
class B(models.Model):
bid = models.AutoField(primary_key=True)
name = models.CharField(max_length=50)
class C(models.Model):
name = models.CharField(max_length=50)
tobid = models.ForeignKey(B)
check = models.datetimeField()
Here is my views.py:
def generated(request,user_id):
f= A()
p = f.something(user_id)
I can perform a for loop that will get all values of class C.
My views to fetch value from C
for des in p:
des_list = des.tob.bid
des_list_final = C.objects.filter(tobid = des_list)
But the problem in here that everytime it takes one id from B and perform the filtering to C. But this will not give me the value of C order by check. Any idea how to overcome this?
C.objects.filter(tobid__id__in=[des.tob.bid.pk for des in p]).order_by('-check')If you want to set the order permanently, use
ordering: