Hi Mike,
-- Thanks for your response.
I am aware of chaining filters.
The latest() API in django queryset returns an object and not a queryset. Hence, I cannot add a filter to it.
I had thought about using chaining filters but couldn't find a way.
On Tuesday, August 30, 2016 at 12:18:56 PM UTC+5:30, Mike Dewhirst wrote:
On Tuesday, August 30, 2016 at 12:18:56 PM UTC+5:30, Mike Dewhirst wrote:
On 30/08/2016 4:33 PM, Web Architect wrote:
> Hi,
>
> I am looking for an elegant and efficient mechanism to have a query
> filter or a solution for the following one to many relationship model.
> Please note the following is just an illustration of the models - hope
> it should provide what I am looking for:
>
> class A(models.Model):
>
> name = models.CharField(_("Name"), max_length=255, unique=True)
>
>
> class B(models.Model):
>
> text = models.CharField(_("Text"), max_length=255, unique=True)
>
> date_created = models.DateTimeField(_("Date Created"),
> auto_now_add=True)Â
>
> Â a =Â models.ForeignKey(A, related_name='b')
>
>
> To get all the instances of B associated with a specific instance of A
> (say 'a'), I could do the following : a.b.all()
>
> The latest instance of B associated with 'a' would be :
> a.b.latest('date_created')
>
> Now I would like to have a list of all instances of A where the latest
> instance of B associated with each instance of A will have the 'text'
> field as 'ABCD'.Â
>
> A.objects.filter(b__text='ABCD') will give all instances of A where
> each instance of A will have atleast one instance of B with 'text' =
> 'ABCD'.
>
> There could be a brute force way of getting a solution for the above
> where in I can do A.objects.filter(b__text='ABCD') Â and then go
> through each instance  of A in a for loop over the queryset and check
> for the latest instance of B for text='ABCD'.
HAve you looked at ...
https://docs.djangoproject.com/en/1.8/topics/db/queries/# chaining-filters
>
> As mentioned earlier, I am looking for an elegant and optimal way (if
> any in Django) for the above query.
>
> Thanks.
>
> Â
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users+unsubscribe@googlegroups.com >.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com >.
> Visit this group at https://groups.google.com/group/django-users .
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/a9ea312a- 578e-4a2f-a739-a38e87a4b15b% 40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/a9ea312a- >.578e-4a2f-a739-a38e87a4b15b% 40googlegroups.com?utm_medium= email&utm_source=footer
> For more options, visit https://groups.google.com/d/optout .
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/508c9991-7d21-4583-82e5-1abea0584511%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment