Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django ORM: See if a model has no foreign key entry in another model

So , I have this 2 models:

class Site(models.Model):
    ...
    ...

and another one:

class SiteInfo(models.Model):
    ...
    ...
    site = models.ForeignKey(Site)

Is there a way to get the Sites that have no entry in the SiteInfo ?

like image 464
Nicu Surdu Avatar asked May 23 '11 08:05

Nicu Surdu


People also ask

Does Django automatically index foreign keys?

Django automatically creates an index for all models. ForeignKey columns. From Django documentation: A database index is automatically created on the ForeignKey .

Is ForeignKey in Django one to many?

To handle One-To-Many relationships in Django you need to use ForeignKey . The current structure in your example allows each Dude to have one number, and each number to belong to multiple Dudes (same with Business).

What does ForeignKey mean in Django?

What is ForeignKey in Django? ForeignKey is a Field (which represents a column in a database table), and it's used to create many-to-one relationships within tables. It's a standard practice in relational databases to connect data using ForeignKeys.


1 Answers

Site.objects.filter(siteinfo__isnull=True)

like image 95
DrTyrsa Avatar answered Sep 20 '22 08:09

DrTyrsa