Django multiple models that have a foreign key to another model
Example:
A system that processes different types of csv files. The different types of csv files have different content inside.
So the current way im thinking about modeling this is:
File model that stores some generic info about each file processed, like the name, date etc.
class File(models.Model):
name = models.CharField()
Then models that stores the content for each type of file and these models link back to the File model:
class Example1File(models.Model):
file = models.ForeignKey(File, related_name=example_file_1_content)
... (all columns for this file)
class Example2File(models.Model):
file = models.ForeignKey(File, related_name=example_file_2_content)
... (all columns for this file)
Is there an easy way for example to loop through the files table and get the content for each file and django would just know in which table to go look?
for file in File.objects.all():
file.???
Or would I only be able to do this by storing a type field on the file and then getting the content with the reverse relationship like this.
for file in File.objects.all():
if file.type == FileTypes.ExampleFile1:
file.example_file_1_content
But what if there are a lot of different file types? Im still in the process of learning about how django handles relationships/reverse relationships so I might just not be there yet or the way I am modeling this is not optimal.
Thanks in advance.