skip to Main Content

I am making a site in which user uploads it’s pdf and back of the site we generate audio file for each page and now we want to save it on the database(mysql).
Now the problem starts we have model in django and model have the fields which are for a single input.
for example

class Info(models.Model):
    phone_img = models.ImageField(upload_to='pics')
    Name = models.CharField(max_length=70)
    RAM = models.IntegerField(default= None)
    ROM = models.IntegerField(default= None)
    Cost = models.IntegerField(default= None)
    Link = models.URLField(max_length=250)

Now if we use models.FileField(upload_to='files') then it will upload files to a files folder but I have several files then how to upload that files, because we can’t create fields for that and if we create fields for that then it’s not fix that the user uploaded pdf have the same no. of pages as we have fields.

2

Answers


  1. Create another model "PhoneIMG" with a ForeignKey to "Info". Then you can have as many files as you want that are connected to the "Info" model.

    Login or Signup to reply.
  2. You need to create another model to store that files and linked that model to your Infor model as ManyToMany field. for example

    class UploadFile(models.Model):
        phone_img = models.ImageField(upload_to='/location')
        
        def __str__(self):
            return self.id
    

    and then link the model with your Info model as follow

    class Info(models.Model):
        phone_img = models.ManyToManyField(UploadFile)
        Name = models.CharField(max_length=70)
        RAM = models.IntegerField(default= None)
        ROM = models.IntegerField(default= None)
        Cost = models.IntegerField(default= None)
        Link = models.URLField(max_length=250)
    

    for more information please visit the official documentation

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search