Monday, September 4, 2017

How to add more fields to each ManyToMany Field in Django?

Hi,







Is it possible to add one or more field (Char Field) to the options of ManyToMany field?

My current models result in this:



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:

  • University 1
  • University 2
  • University 3


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?


I asked this question on StackOverflow (link) and through one answer, I became aware of through models in ManyToMany field and on using through models, I get no such table error.


You might be asking why do I need this? As you can see in the image, my project (personal project) is about universities etc. What I want is that If I add a course and then choose a university (from ManyToMany Field), I should have an option to enter the fees of that course for that particular university.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f9efd14f-3d82-4032-a425-48d3b0dfaede%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment