V
V
Vitaly2016-10-22 13:09:29
Django
Vitaly, 2016-10-22 13:09:29

How do I organize a bunch of models in creating a photo album?

Welcome all.
I need to make a photo album, and the task arose to correctly organize a bunch of one object with another. I found an example
on StackOverFlow ( here ). It turns out you need to make the Album model with ForeignKey on the image model.
Here's what happened:

class Album(models.Model):
    name = models.CharField(max_length=35)
    owner = models.ForeignKey(User)
    images = models.ForeignKey(Image)
    create_date = models.DateTimeField(auto_now_add=True)

class Image(models.Model):
    image = models.ImageField()
    upload_date = models.DateTimeField(auto_now_add=True)
    slug = models.SlugField()
    views = models.IntegerField(default=0)

How to get photos from an album is clear, but how can I find out what album it is from a photo?
If there is a better way to accomplish this task, please let me know.
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2016-10-22
@sim3x

Relationship Photo is in Album, not Album is in Photo
https://docs.djangoproject.com/en/dev/ref/models/f...

class Album(models.Model):
    name = models.CharField(max_length=35)
    owner = models.ForeignKey(User)
    create_date = models.DateTimeField(auto_now_add=True)

class Image(models.Model):
    album = models.ForeignKey(Image, related_name='images')
    image = models.ImageField()
    upload_date = models.DateTimeField(auto_now_add=True)
    slug = models.SlugField()
    views = models.IntegerField(default=0)

https://docs.djangoproject.com/en/dev/topics/db/qu...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question