I have the following models:
class Category(models.Model): label = models.CharField(max_length=40) description = models.TextField() class Rating(models.Model): review = models.ForeignKey(Review, on_delete=models.CASCADE) category = models.ForeignKey(Category, on_delete=models.CASCADE) rating = models.SmallIntegerField() class Review(models.Model): author = models.ForeignKey(User, related_name="%(class)s_author", on_delete=models.CASCADE) coach = models.ForeignKey(User, related_name="%(class)s_coach", on_delete=models.CASCADE) comments = models.TextField()
I’d like to create a front-end form which allows a user to review a coach, including a rating for some pre-populated categories.
In my head, the form would look something like:
Coach: _______________ # Selection of all coach users from DB, this works as standard Category: "Professionalism" # These would be DB entries from the Category model Rating: _ / 5 Category: "Friendliness" Rating: _ / 5 Category: "Value" Rating: _ / 5 Comments: _________________________________ _________________________________ Submit
I’ve seen Django Formsets in the documentation but these appear to exist for creating multiple forms from the same model as a single form?
Not looking for a full answer, but if someone could point me in the right direction, it’d be hugely appreciated.
EDIT: Vineet’s answer (https://stackoverflow.com/a/65883875/864245) is almost exactly what I’m looking for, but it’s for the Admin area, where I need it on the front-end.
Advertisement
Answer
Given that the categories are fairly static, you don’t want your users selecting the categories. The categories themselves should be labels, not fields for your users to select.
You mention in the comment, that the labels will sometimes change. I think there are two questions I would ask before deciding how to procede here:
- Who is going to update the labels moving forwards (do they have basic coding ability, or are they reliant on using something like the admin).
- When the labels change, will their fundamental meaning change or will it just be phrasing
Consideration 1
If the person changing the labels has a basic grasp of django, and the appropriate permissions (or can ask a dev to make the changes for them) then just hard-coding these 5 things is probably the best way forward at first:
class Review(models.Model): author = models.ForeignKey(User, related_name="%(class)s_author", on_delete=models.CASCADE) coach = models.ForeignKey(User, related_name="%(class)s_coach", on_delete=models.CASCADE) comments = models.TextField() # Categories go here... damage = models.SmallIntegerField( help_text="description can go here", verbose_name="label goes here" ) style = models.SmallIntegerField() control = models.SmallIntegerField() aggression = models.SmallIntegerField()
This has loads of advantages:
- It’s one very simple table that easy to understand, instead of 3 tables with joins.
- This will make everything up and down your code-base simpler. It’ll make the current situation (managing forms) easier, but it will also make every query, view, template, report, management command, etc. you write easier, moving forwards.
- You can edit the labels and descriptions as and when needed with
verbose_name
andhelp_text
.
If changing the code like this isn’t an option though, and the labels have to be set via something like the django admin-app, then a foreign-key is your only way forward.
Again, you don’t really want your users to choose the categories, so I would just dynamically add them as fields, rather than using a formset:
class Category(models.Model): # the field name will need to be a valid field-name, no space etc. field_name = models.CharField(max_length=40, unique=True) label = models.CharField(max_length=40) description = models.TextField()
class ReviewForm.forms(forms.Form): coach = forms.ModelChoiceField() def __init__(self, *args, **kwargs): return_value = super().__init__(*args, **kwargs) # Here we dynamically add the category fields categories = Categories.objects.filter(id__in=[1,2,3,4,5]) for category in categories: self.fields[category.field_name] = forms.IntegerField( help_text=category.description, label=category.label, required=True, min_value=1, max_value=5 ) self.fields['comment'] = forms.CharField(widget=forms.Textarea) return return_value
Since (I’m assuming) the current user will be the review.author
, your going to need access to request.user
and so we should save all your new objects in the view rather than in the form. Your view:
def add_review(request): if request.method == "POST": review_form = ReviewForm(request.POST) if review_form.is_valid(): data = review_form.cleaned_data # Save the review review = Review.objects.create( author=request.user, coach=data['coach'] comment=data['comment'] ) # Save the ratings for category in Category.objects.filter(id__in=[1,2,3,4,5]): Rating.objects.create( review=review category=category rating=data[category.field_name] ) # potentially return to a confirmation view at this point if request.method == "GET": review_form = ReviewForm() return render( request, "add_review.html", { "review_form": review_form } )
Consideation 2
To see why point 2 (above) is important, imagine the following:
- You start off with 4 categories: Damage, Style, Control and Agression.
- Your site goes live and some reviews come in. Say Coach Tim McCurrach gets a review with scores of 2,1,3,5 respectively.
- Then a few months down the line we realise ‘style’ isn’t a very useful category, so we change the label to ‘effectiveness’.
- Now Tim McCurrach has a rating of ‘1’ saved against a category that used to have label ‘style’ but now has label ‘effectiveness’ which isn’t what the author of the review meant at all.
- All of your old data is meaningless.
If Style is only ever going to change to things very similar to style we don’t need to worry so much about that.
If you do need to change the fundamental nature of labels, I would add an active
field to your Category
model:
class Category(models.Model): field_name = models.CharField(max_length=40, unique=True) label = models.CharField(max_length=40) description = models.TextField() active = models.BooleanField()
Then in the code above, instead of Category.objects.filter(id__in=[1,2,3,4,5])
I would write, Category.objects.filter(active=True)
. To be honest, I think I would do this either way. Hard-coding ids in your code is bad-practice, and very liable to going wrong. This second method is more flexible anyway.