Django: foreign key queries

这一生的挚爱 提交于 2020-01-14 07:37:10

问题


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!


回答1:


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



来源:https://stackoverflow.com/questions/4062955/django-foreign-key-queries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!