Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: foreign key queries

I'm learning Django and trying to get the hang of querying foreign keys across a bridging table. Apologies if this is a duplicate, I haven't been able to find the answer by searching. I've got models defined as follows

class Place(models.Model):
    id = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=100)
class PlaceRef(models.Model):
    place = models.ForeignKey(Place) # many-to-one field
    entry = models.ForeignKey(Entry) # many-to-one field
class Entry(models.Model):
    id = models.IntegerField(primary_key=True)
    name =  models.CharField(max_length=10)

If I want to retrieve all the Entries associated with a particular Place, how do I do it?

place = get_object_or_404(Place, id=id)
placerefs = PlaceRef.objects.filter(place=place)
entries = Entry.objects.filter(id.....)

Also, if there is a more sensible way for me to define (or get rid of) PlaceRefs in Django, please feel free to suggest alternatives.

Thanks for helping out a beginner!

like image 521
AP257 Avatar asked Oct 31 '10 11:10

AP257


People also ask

How does ForeignKey work in Django?

ForeignKey is a Django ORM field-to-column mapping for creating and working with relationships between tables in relational databases. ForeignKey is defined within the django.

How can I get ForeignKey ID in Django?

Just add str function in your foreign reference model Product.

What is __ str __ In Django model?

str function in a django model returns a string that is exactly rendered as the display name of instances for that model.


1 Answers

First, I'd suggest rewriting the models to:

class Place(models.Model):
  id = models.IntegerField(primary_key=True)
  name = models.CharField(max_length=100)
class Entry(models.Model):
  id = models.IntegerField(primary_key=True)
  name =  models.CharField(max_length=10)
  places = models.ManyToManyField(Place, related_name='places')

So you can query:

Entry.objects.filter(places__id=id)

In your current model:

Entry.objects.filter(placeref_set__place__id=id)

Note that the double underscore __ is used to jump from one model to the next. Also, django creates some fields on the model that help you navigate to related objects. In this example: Entry.placeref_set. You can read more about it here:

http://docs.djangoproject.com/en/dev/topics/db/queries/#following-relationships-backward

like image 161
OmerGertel Avatar answered Oct 24 '22 15:10

OmerGertel