B
B
Bjornie2017-03-28 00:49:19
Django
Bjornie, 2017-03-28 00:49:19

Should I include SQLAlchemy?

My test project has models whose fields have foreign keys. However, I ran into the problem of compiling a simple queryset using JOIN via Django ORM. I've been trying to figure this out for a week now, and even asked here and on other resources, but I didn't get a clear answer. Partially found solutions through filter / extra, but it did not work everywhere I needed.
Once again, when I googled, I came across a similar question on Stackoverflow, where they recommend trying SA to execute queries like mine (and there is just select + join one field of another table).
Is it worth a try? Is it reasonable? In theory, I know that I would have to master it sooner or later. But will it work at this stage?
If anything, I have 2 models:

class Names(models.Model):
    name = models.CharField(max_length=30)

    class Meta:
        managed = False
        db_table = 'names'
        ordering = ['pk']

class People(models.Model):
    name = models.ForeignKey(Names, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'people'
        ordering = ['pk']

And I need to output the name from Names People.

A piece of SQL for the People model:

INDEX `FK_people_names` (`name_id`),
  CONSTRAINT `FK_people_names` FOREIGN KEY (`name_id`) REFERENCES `names` (`id`)

PS I would have written pure SQL queries long ago, but for some reason I want to do it with ORM.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2017-03-28
@sim3x

https://docs.djangoproject.com/en/1.10/ref/models/...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question