14DENDIK
14DENDIK

Reputation: 9

Storing several informations in one variable in Django Models

    class Cities(models.Model):
    city_main_image = models.FileField()
    city_name = models.CharField(max_length=200)
    city_info = models.CharField(max_length=1000)
    city_images = models.FileField()

In my models.py I have Cities class and I want to upload several images for this class variable, to be clearly for city_images variable how can I do this? Are there any way to do this or not?

Upvotes: 1

Views: 187

Answers (3)

Paandittya
Paandittya

Reputation: 935

class Cities(models.Model):
    city_main_image = models.FileField() 
    city_name = models.CharField(max_length=200)
    city_info = models.CharField(max_length=1000)
class CityImages(models.Model):
    city_id = models.ForeignKey(Cities)
    city_images = models.FileField()

Now each of your city in Cities can have one or more images in another model called CityImages. If we talk in terms of tables then the primary key for a row in table Cities would be associated to one or more rows in table city_images. I will strongly suggest you to go through official introductory tutorial of django. Also I personally find this tutorial very helpful for beginners. Just in case it helps.

Upvotes: 1

user1301404
user1301404

Reputation:

Few notes about your code before answering your question.

1) Stick to singular model names, City rather than Cities.
2) Don't repeat the model name in every field, so you get "main_image", "name", "info", "images".
3) Use ImageField for images rather than FileField.
4) No need to have 2 fields for main_image and images. You can add an extra field to make the image a main image or not.

Now, to answer your question, you need to read about relations in an SQL database. To use relations between your models with django's ORM, look at https://docs.djangoproject.com/en/2.0/ref/models/fields/#django.db.models.ForeignKey

You have 2 options: ForeignKey or ManyToManyField. Stick with the ForeignKey as you don't need a many to many relation.

So you'll have something like the following:

class City(models.Model):
    ...

class CityImage(models.Model):
    image = models.ImageField(...)
    city = models.ForeignKey(City) # you might need to add on_delete parameter depending on your django version.

or

class CityImage(models.Model):
    image = models.ImageField(...)

class City(models.Model):
    ...
    images = models.ManyToManyField(CityImage)

Upvotes: 1

miketheredherring
miketheredherring

Reputation: 21

One way you could do this is to make a CityImage model, which would allow you to make a ForeignKey to the City model, with a related_name=images for the reverse lookup.

class City(models.Model):
    name = models.CharField(max_length=200)
    info = models.CharField(max_length=1000)

    @property
    def main_image(self):
        try:
            return self.images.get(primary=True)
        except CityImage.DoesNotExist:
            pass
        except CityImage.MultipleObjectsReturned:
            # Handle this case, the application should ensure only one is set to `True` at a time...

class CityImage(models.Model):
    city = models.ForeignKey(City, related_name='images')
    primary = models.BooleanField(default=False)
    image = models.FileField()

Upvotes: 0

Related Questions