Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8059013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:31:57+00:00 2026-06-05T09:31:57+00:00

I have been trying for about a day and a half to get the

  • 0

I have been trying for about a day and a half to get the ID of a related field in a model to display in my template. Nothing fancy, I just want the ID.

Here is the model in question:

class CompositeLesson(models.Model):
    lesson = models.ForeignKey(Lesson)
    student = models.ForeignKey(Student)
    actualDate = models.DateTimeField()

Assume I have a list, lessonsList, of CompositeLesson and am able to successfully iterate through the list. Other fields (i.e. actualDate) display correctly.

Snippet of template code:

{% for lesson in lessonsList %}
<tr{% if forloop.counter|divisibleby:"2" %} class="shaded_row"{% endif %}>
    <td>{{ lesson.actualDate }}</td>
    <td class="table_button">
    {% if not lesson.isCancelled %}
        <div class="table_button_div" id="cancel_{{ lesson__lesson__id }}"><a href="#">Cancel Lesson</a></div>
    {% else %}
        <div class="cancelled_lesson"></div>
    {% endif %}

The problem is I can’t get the ID of the Lesson object that is in the list. I’ve tried:

lesson.lesson
lesson.lesson.id
lesson.lesson__id
lesson__lesson__id
lesson.lesson.get_id

…and none of them work.

Thanks in advance!

Edit: here is my view that builds the lesson, per request:

def all_student_lessons(request, id):
    # This should list all lessons up to today plus the next four, and call out any cancelled or unpaid lessons
    # User should have the option to mark lessons as paid or cancel them
    s = Student.objects.get(pk = id)
    if s.teacher != request.user:
        return HttpResponseForbidden()

    less = Lesson.objects.filter(student = id)
    lessonsList = []
    for le in less:
        if le.frequency == 0:
            # lesson occurs only once
            x = CompositeLesson()
            x.lessonID = le.id
            x.studentID = id
            x.actualDate = datetime.combine(le.startDate, le.lessonTime)
            x.isCancelled = False
            try:
                c = CancelledLesson.objects.get(lesson = le.id, cancelledLessonDate = le.startDate)
                x.canLesson = c.id
                x.isCancelled = True
            except:
                x.canLesson = None
            try:
                p = PaidLesson.objects.get(lesson = le.id, actualDate = le.startDate)
                x.payLesson = p.id
            except:
                x.payLesson = None
            lessonsList.append(x)
        else:
            sd = next_date(le.startDate, le.frequency, le.startDate)
            while sd <= date.today():
                x = CompositeLesson()
                x.lessonID = le.id
                x.studentID = id
                x.actualDate = datetime.combine(sd, le.lessonTime)
                x.isCancelled = False
                try:
                    c = CancelledLesson.objects.get(lesson = le.id, cancelledLessonDate = le.startDate)
                    x.canLesson = c.id
                    x.isCancelled = True
                except:
                    x.canLesson = None
                try:
                    p = PaidLesson.objects.get(lesson = le.id, actualDate = le.startDate)
                    x.payLesson = p.id
                except:
                    x.payLesson = None
                lessonsList.append(x)
                sd += timedelta(le.frequency)
    lessonsList.sort(key = lambda x: x.actualDate)
    return render_to_response('manage_lessons.html', {'lessonsList': lessonsList,
                                                    's': s})
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T09:31:59+00:00Added an answer on June 5, 2026 at 9:31 am

    In your view, you’re never assigning x.lesson . It’s logical then, that lesson.lesson is undefined.

    You should replace x.lessonID = le.id for x.lesson = le

    If that doesn’t work, also try an x.save() before a lessonsList.append(x).

    On a sidenote, your model doesn’t seem to be too well defined, because you’re adding new attributes to it that aren’t defined in the model. Also, you may consider creating and storing the CompositeLesson objects before the view in which they are displayed. You may want to create or modify these objects whenever some other important event occurs, such as the lessons being scheduled, payed or cancelled.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying for about a day now to get an xml feed
I have been trying to get this seemingly simple problem fixed for about half
I've been trying for about a day now to get this method to work.
I have been trying for half a day to loop this countdown function. I
I have been learning about move constructors over the last day or so, trying
I have been trying to know about multilingual site. Where in url should be
So, I have been trying to fix this for about two months. It all
Good evening, I have been trying to wrap my head about this, below, code
I have been trying all afternoon to get the jQuery Sifr Plugin ( http://jquery.thewikies.com/sifr/
Ok, I've been trying to find a solution for this for about half a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.