B
B
BobJack2015-05-03 22:55:16
Django
BobJack, 2015-05-03 22:55:16

How to display the data of two models in django?

Good day! Such a problem: there are models in different applications
In the adminpanel:

class User(AbstractBaseUser, PermissionsMixin):
    username = models.CharField(verbose_name='Ник',  max_length=255, blank = True, null = True)
    email = models.EmailField(
        verbose_name='E-mail',
        max_length=255,
        unique=True,
        db_index=True)
    userPhoto = models.ImageField(verbose_name = 'Фотография',  upload_to='userPhotos/%Y/%m/%d', blank = True, null = True)
    name = models.CharField(verbose_name = 'Имя',  max_length = 20, blank = True)
    patronymic = models.CharField(verbose_name = 'Отчество', max_length = 30, blank = True, null = True)
    surname = models.CharField(verbose_name = 'Фамилия',  max_length = 30, blank = True)
    birthdate = models.DateField(verbose_name = 'Дата рождения',  blank = True, null = True)
    address = models.ForeignKey(Addresses)
    passportSeries = models.CharField(verbose_name = 'Серия паспорта', max_length = 10, blank = True)
    passportNumber = models.IntegerField(verbose_name = 'Номер паспорта', default = 0)
    telephoneNumber = models.CharField(verbose_name = 'Номер телефона', max_length = 20, blank = True)
    is_active = models.BooleanField(verbose_name = 'Активен?', default = True)
    is_admin = models.BooleanField(verbose_name = 'Администратор?', default = False)

    objects = UserManager()

    USERNAME_FIELD = 'email'

    def get_full_name(self):
        return '%s %s %s' % (self.name, self.patronymic, self.surname,)

    def get_short_name(self):
        return self.username

    def __unicode__(self):
        return self.email

    def has_perm(self, perm, obj=None):
        return True

    def has_module_perms(self, app_label):
        return True

    @property
    def is_staff(self):
        return self.is_admin

class Student(User):
    institute = models.ForeignKey(Institutes)
    chair = models.ForeignKey(Chairs)
    academicGroupPrefix = models.CharField(verbose_name = 'Префикс группы', max_length = 10, blank = True, null = True)
    academicGroupNumber = models.IntegerField(verbose_name = 'Номер группы', blank = True, null = True)
    gradebookNumber = models.IntegerField(verbose_name = 'Номер зачётки', blank = True, null = True)
    parents = models.CharField(verbose_name = 'Родители', max_length = 20, blank = True, null = True)
    accessionYear = models.IntegerField(verbose_name = 'Год поступления', blank = True, null = True)
    endYear = models.IntegerField(verbose_name = 'Год окончания', blank = True, null = True)

In grades:
class RatingActive(models.Model):
    student = models.ForeignKey(Student, on_delete = models.CASCADE)
    subject = models.ForeignKey(Subjects, on_delete = models.CASCADE, null = True)
    ratingFiveMark = models.IntegerField()
    ratingNationalMark = models.CharField(max_length = 19)
    ratingECTS = models.IntegerField()
    hoursOfAbsences = models.CharField(max_length = 5)
    def __str__(self):
        return self.student.name

I created another application "student" in which I want to display student grades.
I created an authorization, but I can't figure out how to make it in the view so that the values ​​of the logged-in user's ratings are displayed in the user part of the site. Those. I need to display data from the Student class (name, etc.) and from the RatingActive class into the template. Help me please.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2015-05-03
@BobJack

https://docs.djangoproject.com/en/1.8/ref/request-...
Something seems to me not to be inherited from the user here,
but

def view_foo(request):
    return render(request, 'foo.html', {
    'student': Student.objects.get(pk=request.user.pk),
    'rates': RatingActive.objects.filter(pk=request.user.pk)
})

foo.html
<h1>{{ student.get_full_name() }}
<ul>
{% for r in rates %}
<li>{{ r.subject }} - {{ r.rating_five_mark }}
{% endfor %}

incorrect passportSeries
correct passport_series
or python3 def __str__(self):
or python 2 def __unicode__(self):
code not tested

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question