0

I'm fairly new to Django. I have a model copy (exam copy of a student), the model copy will contain a student test or exam copy and a mark, usually, i would use a FileField and save the copy to the object, but my problem is that a copy could contain many files (page 1, 2, 3 etc)

I was thinking about using a CharField instead that contains the path to a folder that contains the files for that copy, but I don't have a very good idea on how to do that

and if you have a better way I would for you to share.

here is my model

class VersionCopie(models.Model):
    id_version = models.CharField(db_column='id_Version', primary_key=True, max_length=100)
    numero_version = models.IntegerField(db_column='numero_Version', blank=True, null=True)
    note_copie = models.FloatField(db_column='note_Copie', blank=True, null=True)
    emplacement_copie = models.CharField(db_column='emplacement_Copie', max_length=10000, blank=True, null=True)
    id_copie = models.ForeignKey('Copie', models.CASCADE, db_column='id_Copie', blank=True, null=True)

i just need to know what kind of path i would save to "emplacement_copie"

Mohamed Benkedadra
  • 1,964
  • 3
  • 21
  • 48
  • May you better explain what do you mean my Copy, and a file containing many pages? Why not just upload the file to a FileField? – Walucas Mar 13 '18 at 19:53
  • like an exam copy, i can't use a FileField because 1 exam copy could have many files (many pages) – Mohamed Benkedadra Mar 13 '18 at 19:57
  • Side node: there's no need to define primary key in Django. You're already provided with an integer field named `pk` and you shouldn't override it without reason. – Maciej Dziuban Mar 13 '18 at 19:58
  • I do have a reason, I gave 27 models in total so I'm giving each model a key to avoid confusion, for example, students would be etud1 etud2 etud3 .. etc and copies would be copy1 copy2 copy3 ... etc – Mohamed Benkedadra Mar 13 '18 at 20:00
  • Keys from different models don't interfere with each other. They just count from 1 to N independently. You're just introducing confusion and it might byte you in the future – Maciej Dziuban Mar 13 '18 at 20:05
  • @MaciejDziuban i'm not talking about confusion from a Django point of view, i'm referring to the sysadmin that will be using this app and filling the data...... what would be the issues generated by what i'm doing ? – Mohamed Benkedadra Mar 13 '18 at 20:37
  • You might want to delete this field one day and you won't be able to do it easily. If you want to have unique fields, just use unique=True. Also (this is my speculation), it might slow down querying, because integers are faster to compare than strings. Give this post a read: https://stackoverflow.com/questions/2055784/what-is-the-best-approach-to-change-primary-keys-in-an-existing-django-app – Maciej Dziuban Mar 13 '18 at 20:41

1 Answers1

1

Well, I think this is classical one-to-many relation. You have probably already implemented something like that by doing id_copie = models.ForeignKey(...)

You should create separate model, representing just one file and containing reference to your VersionCopie. You can still access all files from VersionCopy model, reference is created implictly (see this link).

Example code:

class VersionCopyFile(models.Model):
    file = models.FileField( <your arguments> )
    version_copy = models.ForeignKey(VersionCopy, on_delete=models.CASCADE)
Maciej Dziuban
  • 474
  • 3
  • 21
  • that a good idea, but i was hoping for a way to contain it to just that one model – Mohamed Benkedadra Mar 13 '18 at 20:00
  • Django provides you with a Python wrapper for relational database and it transforms your code to SQL code (at least by default). And this is how it's done in SQL, you can't have arrays – Maciej Dziuban Mar 13 '18 at 20:03