django-queryset

how can I order the results of a query filter?

I have this line of code in my views that allows me to display a group of items by date (I've also reversed the order so the most recent displays first): currentlinks = Current.objects.order_by('date_added').reverse()[:5] this works fine — however, when I concatenate the "order_by" code with a filter... currentsources = Current.objec...

Django - Queryset spanning null relationships using Q

Consider the models: #Models class A(models.Model): fieldOfA = models.CharField(max_length = 4) class B(models.Model): fieldOfB = models.CharField(max_length = 4) class C(models.Model): classA = models.ForeignKey(A, blank=True, null=True) classB = models.ForeignKey(B, blank=True, null=True) When I create objects of C, ...

Django: Why don't foreign key lookups automatically use the pk?

I have class Achievement(MyBaseModel): pass class Alias(MyBaseModel): achievements = models.ManyToManyField('Achievement') >>> ach = Achievement.objects.all()[1] This works : >>> Alias.objects.all().filter(achievements__pk__contains=ach.pk).count() 77L But this doesn't : >>> Alias.objects.all().filter(achievements__cont...

Django - Select a random photo from each Album

Am trying to get a random photo from each album from the data created by syncr. The model (abbreviated) looks like this: class Album(models.Model): title = models.CharField(max_length=200) photos = models.ManyToManyField('Photo') class Photo(models.Model): title = models.CharField(max_length=200) I've tried lots of diff...

How to dynamically provide lookup field name in Django query?

I want to look for a certain string in several fields of a Model in Django. Ideally, it would go something similar to: keyword = 'keyword' fields = ['foo', 'bar', 'baz'] results = [] for field in fields: lookup = "%s__contains" results.append(Item.objects.filter(lookup=keyword)) Of course this won't work, as "lookup" can't be ...

Is Concatenating Django QuerySets In A Loop The Right Thing To Do?

I have a Django model called Collection that represents a collection of items (CollectionItem). Each Collection only contains items of a specific type. (CollectionItem has a foreign key to Collection). I want to get all of the CollectionItems that are in public-flagged lists of a specific type and return them sorted by a particular fi...

Simple JOIN in Django

I'm trying to accomplish something akin to twitter on my website, where one user can follow another one. I want to select all User records that are following a User with a given ID. My follow relationship model look like: class Following(models.Model): user = models.ForeignKey(User, related_name='is_following') followed = models.For...

Django - How to get only 2 object for a combination of fields with a queryset

If I have Model.objects.all() I want to get only one object for any content_object=foo, object_id=N. How can I do that? Say I am ordering by -datetime. If I can get only one object in the queryset for any content_type=foo, object_id=N ... it should be the latest.. How to specify that I only want 1 object for any combination of cont...

Django-queryset get one object per field=foo

I have a basic FK to user, call it owner class Baz(models.Model): owner = models.ForeignKeyField(User) .... .... Now, with a queryset of Baz's, is there something that I can chain that will give me only one Baz per owner? ...

How can I filter a date of a DateTimeField in Django?

I am trying to filter a DateTimeField comparing with a date. I mean: MyObject.objects.filter(datetime_attr=datetime.date(2009,8,22)) I get an empty queryset list as an answer because (I think) I am not considering time, but I want "any time". Is there an easy way in Django for doing this? * I have the time in the datetime...

Basic tree in Python with a Django QuerySet

Here's where I'm exposed as the fraud of a programmer I am. I've never created a data tree. Basically, I have a table with four fields: A, B, C, and D. I need to create a tree of unordered lists based on these fields. Ultimately, it would look something like this: A1 B1 C1 D1 D2 C2 D3 D4 B2 C2 D5 D6 C3 D7 D8 A2 B2 C1...

Query a Django queryset without creating a new queryset?

Not even sure I'm stating the question correctly. Here's the situation. I have a queryset generated by accessing the foreign key relationship. Using the standard Blog/Entry models in the Django documentation, let's say I have selected a blog and now have a set of entries: entries = Blog.objects.get(id=1).entry_set.all() So we have ...

Checking for empty queryset in Django

What is the recommended idiom for checking whether a query returned any results? Example: orgs = Organisation.objects.filter(name__iexact = 'Fjuk inc') # If any results # Do this with the results without querying again. # Else, do something else... I suppose there are several different ways of checking this, but I'd like to know h...

How Do I Select all Objects via a Relationship Model

Given the Model: class Profile(models.Model): user = models.ForeignKey(User, unique=True) class Thingie(models.Model): children = models.ManyToManyField('self', blank=True, symmetrical=False) class Relation(models.Model): profile = models.ForeignKey(Profile) thingie = models.ForeignKey(Thingie) How would one return ...

Get a queryset of objects through an intermediary model

I want get all of the Geom objects that are related to a certain content_object (see the function I'm trying to build at the bottom, get_geoms_for_obj() class Geom(models.Model): ... class GeomRelation(models.Model): ''' For tagging many objects to a Geom object and vice-versa''' geom = models.ForeignKey(Geom) content_...

Django: queryset filter for *all* values from a ManyToManyField

Hi (sorry for my bad english :p) Imagine these models : class Fruit(models.Model): # ... class Basket(models.Model): fruits = models.ManyToManyField(Fruit) Now I would like to retrieve Basket instances related to all fruits. The problem is that the code bellow returns Basket instances related to any fruits : baskets = Baske...

Following a foreign-key of a commented item in a Django Queryset

Dear all, I'm working on a Django application allowing one to comment either a Text, its Paragraphs or the comments themselves. I'm using the Comment app bundled with Django, and A Text class instance is made up of Inline Paragraph class instances. I'm desperately looking for a way get back a QuerySet/List of all the Comments related d...

Get records before and after current selection in Django query

It sounds like an odd one but it's a really simple idea. I'm trying to make a simple Flickr for a website I'm building. This specific problem comes when I want to show a single photo (from my Photo model) on the page but I also want to show the image before it in the stream and the image after it. If I were only sorting these streams by...

django queryset excluding entries in second model

Hello, I'm making a little vocabulary-quiz app, and the basic model for a word is this: class Word(models.Model): id = models.AutoField(primary_key=True) word = models.CharField(max_length=80) id_image = models.ForeignKey(Image) def __unicode__(self): return self.word class Meta: db_table = u'word' ...

Django ORM: Optimizing queries involving many-to-many relations

I have the following model structure: class Container(models.Model): pass class Generic(models.Model): name = models.CharacterField(unique=True) cont = models.ManyToManyField(Container, null=True) # It is possible to have a Generic object not associated with any container, # thats why null=True class Specific1(Gen...