I've done a number of searches on here and read through the ManyToMany documentation for Django and think this model type fits my needs. However, given the way this field appears on the admin page, I'm worried I'll have too much data for this type to work correctly for my purpose.
I have two models, Image and Post. I want to set up my models such that an Image can be associated with a number of Posts and the other way around. My concern is that with tens/hundreds of thousands of Posts and Images, the admin page will be unmanageable and take too long to load.
Here's what I have now:
class Image(models.Model):
post = models.ManyToManyField(Post)
image = models.ImageField(upload_to=f, blank=True)
class Post(models.Model):
post_title = models.CharField(max_length=113)
text = models.CharField(max_length=1500)
pub_date = models.DateTimeField('post date')
image = models.ManyToManyField(Image)
Is there a better way to handle this? I thought about creating a separate database table with foreign keys to both Image and Post. Every association between a Post and an Image would have an entry in that table. Would that work?
That would generate two M2M columns. You probably want just one M2M column:
class Image(models.Model):
image = models.ImageField(upload_to=f, blank=True)
class Post(models.Model):
post_title = models.CharField(max_length=113)
text = models.CharField(max_length=1500)
pub_date = models.DateTimeField('post date')
images = models.ManyToManyField(Image)
Querying the reverse relationship would be specified with:
image_instance.post_set.all()
The other part of the question is a DB optimization question and determines what kind of queries you're going to be running against it.
The transfer of the blob data will probably take the longest time, and that would be true if it were stored in a db or a filesystem.