Omer Iqbal
Omer Iqbal

Reputation: 148

Django: Adding more fields to each ManyToMany Field option

Is It possible to add one or more Char Fields to each ManyToMany field option?

enter image description here

My Models:

class engineeringUni(models.Model):
    field2 = models.CharField(max_length=200)
    des_eng = models.CharField(max_length=1000, default='Add description')


    def __str__(self):
        return self.field2

    def description_eng_universities(self):
        return self.des_eng

class engineering_courses(models.Model):
    course_name = models.CharField(max_length=400)
    course_description = models.CharField(max_length=1000, default='This is a description')
    course_offered_by = models.ManyToManyField(engineeringUni, related_name='course_offered_by')
    course_duration = models.IntegerField(blank=False, default='2')

    def __str__(self):
        return self.course_name

    def description_course(self):
        return self.course_description

    def offered_by_courses(self):
        return self.course_offered_by

    def duration_courses(self):
        return str(self.course_duration)

As you can see in the image, I have the options in the ManyToMany field. Those options are:

What I want to have is an additional text (Char) field next to each of these options (University 1, University 2, University 3).

Is this possible?

EDIT 1:

Current code:

class engineering_courses(models.Model):
    course_name = models.CharField(max_length=400)
    course_description = models.CharField(max_length=1000, default='This is a description')
    course_offered_by = models.ManyToManyField(
        engineeringUni, 
        through='ThroughModel', 
        through_fields=('course', 'university'),
        )
    course_duration = models.IntegerField(blank=False, default='2')



    def __str__(self):
        return self.course_name

    def description_course(self):
        return self.course_description

    def offered_by_courses(self):
        return self.course_offered_by

    def duration_courses(self):
        return str(self.course_duration)



class ThroughModel(models.Model):
    course = models.ForeignKey(engineering_courses, on_delete=models.CASCADE)
    university = models.ForeignKey(engineeringUni, on_delete=models.CASCADE)
    additional_text = models.CharField(max_length=200)

EDIT 2: Problem fixed. I was getting that no table error because I had deleted the migration files and on deleting database (db.sqlite3) file and applying migration again, It fixed.

Upvotes: 3

Views: 3868

Answers (2)

user1045680
user1045680

Reputation: 859

Take another look at the django docs referenced in the answer from arjun27. You have more than one foreign key in your ThroughModel, so django is confused. Try specifying the through fields in your engineering_course model, migrate the changes, and see if that works.

Mark

Upvotes: 3

arjunattam
arjunattam

Reputation: 2819

You can use a through model in the ManyToManyField (docs). This model can be used to store any additional fields.

class engineering_courses(models.Model):
    # ...
    course_offered_by = models.ManyToManyField(engineeringUni, related_name='course_offered_by', through='ThroughModel')


class ThroughModel(models.Model):
    course = models.ForeignKey(engineering_courses)
    university = models.ForeignKey(engineeringUni)
    additional_text = models.CharField()

Upvotes: 4

Related Questions