简体   繁体   中英

Make an elements list an attribute of the "Groupe" table, where the elements are records (tuples) of the "Etudiant" table

I create a database where one of the attributes of one table must be a list of tuples (record) from another table. I have a "Etudiant" table and a "Groupe" table in my file. How to do it please?

Here is an excerpt of the code containing the relevant tables.

from django.db import models;
from django.contrib.auth.models import User;
from django.utils import timezone;


# Table qui stockera les groupes aux quelles les étudiants appartiennent
class Groupe(models.Model):

    numero_groupe       = models.IntegerField();
    nombre_de_membre    = models.IntegerField();
    liste_etudiant      = ; # Here an students list;
    chef_de_groupe_tp   = models.CharField(max_length=100);
    ue                  = models.ForeignKey(UE, on_delete=models.PROTECT); 
    date_creation       = models.DateTimeField(default=timezone.now, verbose_name="Date de création");

    class Meta:
        verbose_name    = "groupe";
        ordering        = ['date'];

    def __str__(self):
        info_groupe     = ue.code_ue + " : "  numero_groupe;

        return self.info_groupe;


# Table qui stockera les informations concernant les étudiants
class Etudiant(models.Model):

    matricule_etudiant      = models.CharField(max_length=10);
    user                    = models.OneToOneField(User); # étudiant a désormais les attributs de User.
    niveau                  = models.CharField(max_length=10);
    avatar                  = models.ImageField(null=True, blank=True, upload_to="avatar/etudiant/");
    td                      = models.ManyToManyField(TD, through='Note_TD');
    tp                      = models.ManyToManyField(TP, through='Note_TP');

    class Meta:
        verbose_name        = "étudiant";
        ordering            = ['matricule_etudiant'];

    def __str__(self):
        info_etudiant       = matricule_etudiant + " " + user.last_name + " " + user.firstname + " " + niveau;

        return self.info_etudiant;

As far as I know django does not have a ListField in it. If you are using postgres as your database, you can use ArrayField from django.contrib.postgres.fields . Otherwise you can use solution given below but it has its own limitations. (You can not query like normal field).

import json

class Groupe(models.Model):
    _liste_etudiant = models.TextField()

    @property
    def liste_etudiant(self):
        return json.loads(self._liste_etudiant)

    @property.setter
    def liste_etudiant(self, value):
        self._liste_etudiant = json.dumps(value)

Or you can use JsonField from django.contrib.postgres.fields if using postgres . In that case you can also query on this field.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM