0

I'm new to django.

In my project, I have so far an app with a student model. Model's fields are name grades (ie math, english....) and comments. This is working just fine.

Now I need to add after school activities (ie football, dance, guitar....)

The problem starts here, I need to do a model for the activity which acts like a python dict, activity is the dict, student is the key and hours spend in the activity are the values.

It is typically a M2M relation: an activity has several students, and a student can be in several activities with a different amount of participation in each one.

On stackoverflow I've found this answer: How to store a dictionary on a Django Model? witch is really close but not there yet.

I tried different options. Right now I have this:

In studentapp.models:

class Student(models.Model):

    nom = models.CharField(max_length=50, blank=True)
    #grades are all integer fields
    commentaires = models.TextField(max_length=250, blank=True)

class Edudient(models.Model): #frensh for student BTW
    nom = models.OneToOneField(Etudient, db_index=True, on_delete=True)
    hours = models.IntegerField(default=0)

in activity.models:

class Activity(models.Model):

    nom = models.CharField(max_length=50, help_text="exemple: 'Activity football'")

    def __str__(self):
        return self.nom

class WhoIsActive(models.Model):
    activity = models.ForeignKey(Activity, db_index=True, on_delete=True)
    who = models.ForeignKey(Etudient, db_index=True, on_delete=True)

    def __str__(self):
        return str(self.activity) if self.activity else None

With this I can:

  • create as many Etudient instances as I want all with a separate number of hours
  • After creating them, I can add them to the WhoIsActive
  • Create an activity from WhoIsActive

I can not:

  • Create Etudient directly from WhoIsActive
  • Etudient's instances are Student's query, not instances

I feel like I am pretty close, but I don't know what I am doing wrong. Any ideas?

Thank you for future answers.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Gregory F
  • 23
  • 3
  • 1
    There are lots of things here that don't make sense. Why do you have a separate Etudient model? Why does that model have a one-to-one relationship with itself? Why are you talking about a dict at all when what you need is a many-to-many? And since you know you need a many-to-many, why don't you use a ManyToManyField? – Daniel Roseman Jan 09 '18 at 10:57
  • https://docs.djangoproject.com/fr/2.0/topics/db/models/#extra-fields-on-many-to-many-relationships – bruno desthuilliers Jan 09 '18 at 11:02
  • Thanks you for the answer. First the Etudient model is made to have separate hours for each Activity. Ie 2 hours in football and 1 in Guitar activity. Put hours in student class doesn't allow this. – Gregory F Jan 09 '18 at 16:51
  • I forgot: i tried befor the M2M but without "through" argument. That is why i wasn't working. Now it is. Thanks you again @DanielRoseman – Gregory F Jan 09 '18 at 17:02

0 Answers0