LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-23-2011, 07:39 PM   #1
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Costa Rica
Distribution: Kubuntu, Debian, Knoppix
Posts: 2,092
Blog Entries: 1

Rep: Reputation: 90
django: making a query joining two models


I have worked with joins with django and I'm kind of used to them, however, they normally require you to use filters that use an external value. What is the simplest way to get a filter that only requires joining two models and no external value?

For instance:

Code:
class Model1(models.Model):
    name = models.CharField(max_length=255)

class Model2(models.Model):
    parent = models.ForeignKey(Model1)
    name = models.CharField(max_length=255)
As an example of a query requiring an external value, suppose I'd like to get the Model1 instances that are related to instances of Model2 whose names are 'Foo':

Code:
r = Model1.object.filter(model2__name='Foo')
Good.... but how about if I want to get the instances of Model1 that are related to at least a single instance of a Model2?

Code:
r = Model1.object.filter(pk=model2__parent)
I'm trying something like that, but it doesn't work.
 
Old 12-23-2011, 08:54 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,217

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
Code:
 Model1.objects.filter(model2__isnull=False)
This is the query you want for the example.
 
1 members found this post helpful.
  


Reply

Tags
django, joins, models


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Buttons in django ! noony123 Programming 3 03-21-2011 11:51 AM
Integrating Django Into Apache cmnorton Linux - Server 1 09-24-2010 12:59 PM
Django jokar.mohsen Linux - Software 1 07-12-2010 07:51 AM
Mumble, murmur, django and mumble-django. Linux.tar.gz Linux - Software 2 02-10-2009 02:47 AM
command to query harddisk models sausagejohnson Linux - Hardware 2 08-31-2005 08:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration