0

this is my model:

class Student(models.Model):
   user = models.OneToOneField(User,on_delete=models.CASCADE)
   frist_name = models.CharField(max_length=250)
   last_name = models.CharField(max_length=250)
   father_name = models.CharField(max_length=250)
   national_code = models.CharField(max_length=12)
   date_of_birth = models.DateField()
   phone_regex = RegexValidator(regex=r'(\+98|0)?9\d{9}', message="Phone number must be entered in the format: '+999999999'. Up to 15 digits allowed.",)
   phone_number = models.CharField(validators=[phone_regex], max_length=13, 
   blank=True,help_text='Do like +98913.......') # validators should be a list
   CHOICES = (
    ('male','Male'),
    ('female','Female')
   )
   gender = models.CharField(choices=CHOICES,max_length=6)
   date_created = models.DateTimeField(auto_now_add=True)

   def __str__(self):
       return (self.frist_name)


class Classes(models.Model):
   book = models.CharField(max_length=250)
   grade = models.CharField(max_length=250)
   teacher = models.ForeignKey(Teacher,on_delete=models.CASCADE)
   student = models.ManyToManyField(Student,related_name='student')
   date_created = models.DateTimeField(auto_now_add=True)

   def __str__(self):
       return self.grade

how can i make query to find a user book and grade for a specific student?

For example : the frist name is mohammad and last name is kavosi and username is 0605605605 i want to find the grade and book of this user.

Is my model valid or not?

dmitryro
  • 3,463
  • 2
  • 20
  • 28
  • You can't find THE grade or THE book, as one user can be in multiple classes at the same time. If this is not desired, you have to add a foreign key to `Student` instead of a `ManyToManyField` in `Classes`. – Erich Apr 25 '20 at 16:38
  • Possibly related to https://stackoverflow.com/questions/2218327/django-manytomany-filter – dmitryro Apr 25 '20 at 16:39

1 Answers1

0

First of all, your "Classes" model name should be singular, maybe something like, "Course". And then in this model, I see that it has a "student" manytomanyfield, its related name should be something that the student model use to refer "Course" as, therefore the related name should be "courses".

Therefore your "Classes" model should look like:

class Course(models.Model):
   book = models.CharField(max_length=250)
   grade = models.CharField(max_length=250)
   teacher = models.ForeignKey(Teacher,on_delete=models.CASCADE)
   student = models.ManyToManyField(Student,related_name='courses')
   date_created = models.DateTimeField(auto_now_add=True)

   def __str__(self):
       return self.grade

To answer your question of how to find the grade and books, because the student can be in many different courses, you can get the corresponding grade and book in each course of a student Jane Doe by doing:

student = Student.objects.get(first_name='Jane', last_name='Doe')
courses = student.courses.all()
grade_book_list = couses.values_list('book', 'grade')
MarkL
  • 313
  • 1
  • 9