How to join in django?
view.py
q = Q()
q &= Q(user_idx = request.user.id)
portfolio_list = UserPortfolio.objects.filter(q).order_by('-idx')
q &= Q(portfolio_idx = portfolio_list.values('idx'))
portfolio_img_list = UserPortfolioFile.objects.filter(q).order_by('-idx')
model.py
class UserPortfolio(models.Model):
idx = models.AutoField(primary_key=True)
user_idx = models.ForeignKey(
User,
db_column='user_idx',
on_delete=models.CASCADE
)
subject = models.CharField(max_length=255)
client_name = models.CharField(max_length=255)
client_service = models.CharField(max_length=255)
client_category = models.CharField(max_length=255)
start_date = models.DateTimeField()
end_date = models.DateTimeField()
content = models.TextField()
write_date = models.DateTimeField(auto_now = True)
update_date = models.DateTimeField(auto_now = True)
is_file = models.CharField(max_length=1)
class Meta:
managed = False
db_table = 'account_user_portfolio'
def portfolio_upload_to(instance, filename):
nowDate = datetime.now().strftime("%Y/%m/%d")
return '/'.join([str(instance.portfolio_idx.user_idx), instance.folder , nowDate, filename])
class UserPortfolioFile(models.Model):
idx = models.AutoField(primary_key=True)
portfolio_idx = models.ForeignKey(
UserPortfolio,
db_column='portfolio_idx',
on_delete=models.CASCADE
)
folder = 'portfolio'
file = models.ImageField(upload_to=portfolio_upload_to)
class Meta:
managed = False
db_table = 'account_user_portfolio_file'
I want to get image in my image table.
ex)I want to get portfolio_idx 1 to list in template
But I don't know how to get.
Maybe I think that is almost done. right?
If not please answer.