0

I have a website which catalogs various climbs. These climbs have difficulty grades associated with them, which are non-integer values. Users can also submit reviews of the selected climbs, and assign their own suggested difficulty grade. I want to be able to assign integer values to these difficulties, average them, and then output the result back in terms of the grading system.

For example, if the rating system goes 5.1, 5.2, 5.3 ... 5.9, 5.10, 5.10a, 5.10b etc.. how do I assign an integer value to each of these fields so that I can average them later?

class Climb(models.Model):
    GRADES = (
        ('5.15', '5.15'),
        ('5.14', '5.14'),
        ... (more difficulties here)
        ('5.1', '5.1'),
    )
    name = models.CharField(max_length=255, unique=True)
    grade = models.CharField(max_length=255, choices=GRADES)
    date = models.DateTimeField(auto_now_add=True)
    def __str__(self):
        return "%s %s" % (self.name, self.grade)

I tried the following:

class Climb(models.Model):
    GRADES = (
        ('20', '5.15a'),
        ('19', '5.14d'),
        ('18', '5.14c'),
        ...
        ('1', '5.1'),
    )

But ran into issues later because I have another form where uses can select the climb, and the climb should display ideally as "Climb_name climb_grade" where climb_grade is "5.15a" or "5.9" or whatever, but instead was showing up as "20" or "10" or whatever integer I had attached to that difficulty.

Any ideas?

1 Answer 1

1

If your numbering system works for you, and you are only concerned that the user doesn't see the proper grade of the climb, Django has a nifty little helper for displaying in the templates. Hopefully this is what you're looking for!

Form: ClimbForm.get_grade_display()

Template: {{ climb.get_grade_display }}

Sign up to request clarification or add additional context in comments.

3 Comments

This seems like it would do the trick! Having trouble implementing it though. Can you help troubleshoot? In the form where users submit reviews of the climb, when they go to select the climb, they are calling on the str field from the model. So the display is "Self.name, self.grade", which shows the integer value. I tried changing that to self.grade_display, but that did not work (returns no grade_display attribute).
Nevermind, changed it to '%s %s' % (self.name, self.get_grade_display()) and that did the trick. Thank you!!
Sorry I didn't see your comment earlier, glad you figured it out!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.