python - How to join three tables with Django ORM? -
my models are:
class profiles(models.model): first_name = models.charfield() last_name = models.charfield() email = models.charfield() class meta: db_table = 'users' class products(models.model): title = models.charfield() content = models.charfield() price = models.bigintegerfield() user = models.foreignkey(profiles, related_name="product_owner") # user_id class meta: db_table = 'products' class userfollows(models.model): profile = models.foreignkey(profiles, related_name='profile_following') # profile_id following = models.foreignkey(profiles, related_name='profile_is_following') # following_id class meta: db_table = 'user_follows'
to want, query raw sql query below:
select * products, users, user_follows products.user_id=users.id , user_follows.following_id=products.user_id , user_follows.profile_id=12345;
but need django orm. can join 'users' , 'products' tables select_related have no idea how join 'user_follows'. , need apply filter on field of 'user_follows' table. please help!
Comments
Post a Comment