I have two tables in Django: First one is file which has the file information and second one is share which has the shared information:
files_id is foreign key to Share table:
Now, I want to retrieve file information(from File table) which has files_id in shared table. How can I do that?
#models.py
class File(models.Model):
users = models.ForeignKey(User)
file_name = models.CharField(max_length=100)
type = models.CharField(max_length=10)
source = models.CharField(max_length=100)
start_date = models.TextField()
end_date = models.TextField()
duration = models.TextField()
size = models.TextField()
flag = models.TextField()
#delete_date = models.CharField(max_length=100, null=True, blank=True)
class Share(models.Model):
users = models.ForeignKey(User)
files = models.ForeignKey(File)
shared_user_id = models.IntegerField()
shared_date = models.TextField()
I want to get the file_information with users_id is log_id and get the shared_date too. I get log_id as: log_id = request.user.id
I have made some changes to your models to remove confusion of
plural namesforuserandfile. I think you named them that because you already have some database.So, from above two models, it appears that
Fileobject is created by oneUserwhich can be shared bu other users. Since you havefiles_idsomehow, following query should workWhy are you using
TextFieldfor storingDateTime? You should useDateTimeField.