Alvaro
Alvaro

Reputation: 1448

Many_to_many --> FieldError: Related Field got invalid lookup: contains

I have a two two models: Doctor and Patient. Doctor is defined as follows:

class Doctor(models.Model):
    patients = ManyToManyField('patients.Patient', related_name="%(class)ss", blank=True)

I want to get all the doctors that have a specific patient. I tried:

doctors = Doctor.objects.filter(patients__contains=patient)

it doesnt' seem to work...any idea?

Upvotes: 0

Views: 566

Answers (1)

JPG
JPG

Reputation: 88549

patients expects a integer value, which is not supported for contains query

Try

patient = 1
doctors = Doctor.objects.filter(patients=patient)

or

patients = [1,2,3,4]
doctors = Doctor.objects.filter(patients__in=patients)

Upvotes: 1

Related Questions