Hi,
I have 2 models:

@python_2_unicode_compatible
class Document(models.Model):
    uuid = models.CharField(max_length=32, editable=False, unique=True)
    contractor = models.ForeignKey(Contractor)
    stagcy = models.ForeignKey(StateAgency)
    type = models.CharField(max_length=25)
    year = models.PositiveSmallIntegerField()
    period = models.ForeignKey(Period, null=True, blank=True, 
related_name='+')
    created = models.DateTimeField(auto_now_add=True)

    class Meta:
        unique_together = (
            ('contractor', 'stagcy', 'type', 'year', 'period'),
        )
        ordering = ('-created',)

    def __str__(self):
        return self.uuid


@python_2_unicode_compatible
class Sending(models.Model):
    FLAGS = (
        'F_CREATED',
        'F_QUEUED',
        'F_PREPARED',
        'F_EXPORTED',
        'F_SENT',
        'F_CONFIRMED',
        'F_COMPLETED',
        'F_DELETED',
    )

    document = models.ForeignKey(Document, related_name='sendings')
    uuid = models.CharField(max_length=32, editable=False, unique=True)
    korr = models.PositiveSmallIntegerField(null=True, blank=True)
    is_external = models.BooleanField(default=False)
    user = models.ForeignKey(settings.AUTH_USER_MODEL)
    state = bitfield.BitField(FLAGS, default=('F_CREATED',))
    detail = jsonfield.JSONField(ensure_ascii=False, editable=False)
    created = models.DateTimeField(auto_now_add=True)
    queued = models.DateTimeField(null=True)
    is_export_allowed = models.BooleanField(default=True)
    exported = models.DateTimeField(null=True)
    date_sent = models.DateTimeField(null=True)
    completed = models.DateTimeField(null=True)
    last_modified = models.DateTimeField(auto_now=True)
    remark = models.CharField(max_length=1000, blank=True)
    is_viewed = models.BooleanField(default=False)

    class Meta:
        ordering = ('-created',)

    def __str__(self):
        return self.uuid


Shell
====
>>> queryset = Document.objects.filter(sendings__user=1)
SELECT ...
FROM "document_document"
INNER JOIN "document_sending" ON ("document_document"."id" = 
"document_sending"."document_id")
WHERE "document_sending"."user_id" = 1
ORDER BY "document_document"."created" DESC 

>>>queryset.filter(sendings__state=0b0000001)
SELECT ...
FROM "document_document"
INNER JOIN "document_sending" ON ("document_document"."id" = 
"document_sending"."document_id")
INNER JOIN "document_sending" T4 ON ("document_document"."id" = 
T4."document_id")
WHERE ("document_sending"."user_id" = 1
       AND T4."state" = 1)
ORDER BY "document_document"."created" DESC 

Why...??? 
it is incorrect!!!

must be...

SELECT ...
FROM "document_document"
INNER JOIN "document_sending" ON ("document_document"."id" = 
"document_sending"."document_id")
WHERE ("document_sending"."user_id" = 1
       AND "document_sending"."state" = 1)
ORDER BY "document_document"."created" DESC 


How can I solve this problem?

Thanks
Alexi

-- 
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/d7659d5e-8495-42e1-a037-056fc71309be%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to