问题描述:

I know it's been answered more than couple of times, still from the docs and samples I cant figure out what's wrong with my code:

MODEL:

class BaseAbstractModel(models.Model):

class Meta:

abstract = True

version = models.BigIntegerField(default = -1)

update_time = models.DateTimeField(auto_now=True)

create_time = models.DateTimeField(auto_now_add=True)

def save(self, *a, **kw):

save_lock = LockFactory.get_lock((type(self), self.id,))

with save_lock:

self.version += 1

super(BaseAbstractModel, self).save(*a, **kw)

objects = BaseAbstractModelManager()

class SessionData(BaseAbstractModel):

token = models.SlugField(max_length=20)

class SessionDataTransactions(BaseAbstractModel):

session = models.ForeignKey(SessionData, related_name='transactions')

submitted = models.DateTimeField(null=True)

im trying to carry out left join to do something like:

select main_sessiondata.id,

main_sessiondata.token,

main_sessiondatatransactions.submitted

FROM main_sessiondata

LEFT JOIN main_sessiondatatransactions ON

main_sessiondata.id=main_sessiondatatransactions.session_id;

Have tried 'filter', 'select_related', 'prefetch_related', nothing works.

Is it possible to get all the values from LEFT(sessionData) regardless if matching items found on RIGHT(sessionDataTransactions)?

Any idea?

网友答案:

Django doesn't do joins on reverse relationships, so the behaviour you describe is effectively the default. SessionData.objects.all().prefetch_related('transactions') will have the same result in two queries.

相关阅读:
Top