Good day for Everyone. Because I have self-reference, my queryset no work properly I have a model
class Agency(Organization): organization_ptr = models.OneToOneField(to='Organization', parent_link=True, primary_key=True, related_name='%(class)s', serialize=False, on_delete=CASCADE) accreditation = models.OneToOneField('Accreditation', null=True, blank=True, on_delete=DO_NOTHING) parent = models.ForeignKey("self", null=True, blank=True, on_delete=SET_NULL) class Application(models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) name = models.CharField(max_length=255) seers = models.ManyToManyField('Agency', blank=True, through='ApplicationAgencySeer') # and other class ApplicationAgencySeer(models.Model): application = models.ForeignKey(Application, on_delete=models.CASCADE) agency = models.ForeignKey('Agency', on_delete=models.CASCADE) status = models.IntegerField('Status', choices=CHOICE) created = models.DateTimeField(auto_now_add=True) Now I wanna exclude (simple query) a = Application.objects.exclude(seers__agency__id='9e71cff4-443d-4c60-ac2d-9dcca2a9c147') print(a.query) " * " change by myself SELECT * FROM "nextcert_application" WHERE NOT ("nextcert_application"."id" IN (SELECT U1."application_id" FROM "nextcert_applicationagencyseer" U1 INNER JOIN "nextcert_agency" U2 ON (U1."agency_id" = U2."organization_ptr_id") INNER JOIN "nextcert_agency" U3 ON (U2."organization_ptr_id" = U3."parent_id") WHERE U3."organization_ptr_id" = '9e71cff4-443d-4c60-ac2d-9dcca2a9c147')) ORDER BY "nextcert_application"."created_date" DESC; result application_id 7d83d056-5a7d-4095-9037-98bde29a3d78 otherfields... otherfields.. 7cb60afc-109d-4570-ad24-6cad6b7ddd9a otherfields... otherfields.. <-- this row error exclude - no really exclude Invoked problem by `INNER JOIN "nextcert_agency" U3 ON (U2. "organization_ptr_id" = U3."parent_id")` --return 0 (SELECT U1."application_id" FROM "nextcert_applicationagencyseer" U1 INNER JOIN "nextcert_agency" U2 ON (U1."agency_id" = U2."organization_ptr_id") INNER JOIN "nextcert_agency" U3 ON (U2."organization_ptr_id" = U3."parent_id") WHERE U3."organization_ptr_id" = '9e71cff4-443d-4c60-ac2d-9dcca2a9c147') --althouth I have myapp_applicationagencyseer id created agency_id application_id status 1 2018-12-10 17:41:14.272684 9e71cff4-443d-4c60-ac2d-9dcca2a9c147 7cb60afc-109d-4570-ad24-6cad6b7ddd9a 1 2 2018-12-11 19:25:58.818000 9e71cff4-443d-4c60-ac2d-9dcca2a9c147 7cb60afc-109d-4570-ad24-6cad6b7ddd9a 0 -- myapp_agency organization_ptr_id accreditation parent aff44d42-ce81-4c3e-b6e1-056ed9351adb Null Null 9e71cff4-443d-4c60-ac2d-9dcca2a9c147 10АА71 Null <-- It have Null parent Why I have `INNER JOIN "nextcert_agency" U3 ON (U2."organization_ptr_id" = U3."parent_id")` . How to me be? Maybe It is Django bug? -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6fc2cc66-6afe-41cd-a2de-e76a4f8cd4d7%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.