简体   繁体   中英

Find pk in queryset Django

I have a problem in obtaining a single id from a queryset. I post my models and views in order to be more clear: models.py

class MissionEntry(models.Model):
    student = models.ForeignKey(
        Student, on_delete=models.DO_NOTHING, blank=True, null=True)
    mission = models.ForeignKey(
        Mission, on_delete=models.DO_NOTHING, null=True, blank=True)
    log_entry = models.ForeignKey(
        LogEntry, on_delete=models.DO_NOTHING, blank=True, null=True)
    learning_objective = models.ForeignKey(
        LearningObjective, on_delete=models.DO_NOTHING, blank=True, null=True)
    grade = models.CharField(
        max_length=10, choices=GRADING_VALUE, blank=True, null=True)
    note = models.TextField(blank=True, null=True)
    debriefing = models.TextField(blank=True, null=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return str(self.mission) + ' ' + str(self.log_entry)

    class Meta:
        verbose_name_plural = 'Mission Entries'


class MissionEntryStatus(models.Model):
    mission = models.ForeignKey(
        Mission, on_delete=models.PROTECT, null=True, blank=True)
    student = models.ForeignKey(Student, on_delete=models.PROTECT)
    is_completed = models.BooleanField(default=False)
    is_failed = models.BooleanField(default=False)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)


class StudentMission(models.Model):
    mission = models.ForeignKey(Mission, on_delete=models.PROTECT)
    student_training_course = models.ForeignKey(
        StudentTrainingCourse, on_delete=models.PROTECT)
    mission_status = models.ForeignKey(
        MissionEntryStatus, on_delete=models.PROTECT, blank=True, null=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        ordering = ['mission__name']

    def __str__(self):
        return self.mission.name

class LogEntry(models.Model):
    aircraft = models.ForeignKey(Aircraft, on_delete=models.DO_NOTHING)
    adep = models.ForeignKey(
        Aerodrome, on_delete=models.PROTECT, related_name='adep')
    ades = models.ForeignKey(
        Aerodrome, on_delete=models.PROTECT, related_name='ades')
    date = models.DateField()
    etd = models.TimeField()
    ata = models.TimeField()
    eet = models.TimeField()
    function_type = models.ForeignKey(FunctionType, on_delete=models.PROTECT)
    student = models.ForeignKey(
        Student, on_delete=models.PROTECT, blank=True, null=True)
    instructor = models.ForeignKey(
        Instructor, on_delete=models.PROTECT, blank=True, null=True)
    student_mission = models.ForeignKey(
        'mission.StudentMission', on_delete=models.PROTECT, null=True, blank=True)
    note = models.TextField(null=True, blank=True)
    cross_country = models.BooleanField(default=False)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

try this:

log_entry = LogEntry.objects.get(student_mission_id__in=missions)
print(log_entry.id)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM