Override the template of a widget and pass values to it with Django 4.0

Tiziano Pedrazzoli

I am creating a simple app to do product reviews. I have a Product, Manufacturer and Review models which I will now summarize

class Manufacturer(models.Model):
    name = models.CharField()

class Product(models.Model):
    name = models.CharField()
    manufacturere = models.ForeignKey(Manufacturer)

class Review(models.Model):
    title = CharField()
    product = models.ForeignKey(Product)

I created a ModelForm to create the review. So far I was only able to create a custom widget with an overridden template.

class CustomSelectWidget(forms.widgets.Select):
    option_template_name = 'widgets/select_option.html'

class NewReviewForm(forms.ModelForm):
    review = forms.ModelMultipleChoiceField(
        queryset = Review.objects.none(),
        widget = CustomSelectWidget(),
    )
    class Meta:
        model = Review
        fields = '__all__'

The form view is a normal function based view. The review field is rendered with a list of products in a <select>.What I want to do is change the text inside <option> to make it so that the manufacturer's name is also there. IMPORTANT: I have to take the manufacturer's name from the respective Object, I can't just insert it into the Product name field. ES:

what I have now

  • Geforce RTX2080
  • MAXSUN AMD Radeon RX 550
  • Arca770

the goal

  • NVIDIA - Geforce RTX2080
  • AMD - MAXSUN Radeon RX 550
  • INTEL - Arca770
Lucas Grugru

You can override create_option in the widget:

class CustomSelectWidget(forms.widgets.Select):
    option_template_name = 'widgets/select_option.html'

    def create_option(self, name, value, label, selected, index, subindex=None, attrs=None):
        label = f"{value.instance.product.name} {value.instance.title}"
        return super().create_option(name, value, label, selected, index, subindex, attrs)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Django widget override template

Override django admin landing page and pass variables to template

get checkbox values in django template and pass to view

Django widget template override does not search in the project template directory. How to fix?

Override template in django admin

how to pass many values through the same variable in Django template

How to pass values of drop down from html template to views in Django?

Helm Override common template values

Django, override template block conditionally

How can i pass data from django template to angular 4?

Count True or False (1 or 0) values from database in Django template

Django pass variable into template

Pass variable in to template in django

Django use Widget to Override ModelForm Attributes

How to override a Model Form widget in Django?

Django pass model to widget in ModelForm

Blogspot Template: pass widget output to javascript

Comparing values in django template

unique values in django template

How to override ModelChoiceField / ModelMultipleChoiceField default widget with a template for each choice

How to pass multiple values from template to template?

Django : How to override the CSRF_FAILURE_TEMPLATE

Django: efficient template/string separation and override

Django - how to override form template tag in html

Symfony4 Override FosUserBundle Template

Pass pk into another template in Django

Django queryset with for loop and pass to template

is it possible to pass template variable to django template tag

Pass widget values within ui in Shiny