简体   繁体   中英

How to display a ModelChoiceField's Selected Value in Django

I need to display just the selected value from a forms.ModelChoiceField on a view page. How would I do that?

I've looked at many different forums and couldn't get a clear answer on what I should do in my case. I am new at Python.

form:

class Manufacturer1Form(ReadOnlyFormMixin, ModelForm):
    manufacturer = forms.ModelChoiceField(queryset=Vendor.objects.filter(vendor_type='manufacturer').order_by('name'))

    class Meta:
        model = Manufacturer1Relationship
        exclude = ('part',)

model:

class Manufacturer1Relationship(models.Model):
    part = models.ForeignKey(Part, on_delete=models.CASCADE)
    manufacturer = models.ForeignKey(Vendor, on_delete=models.CASCADE,
                                     limit_choices_to={'vendor_type': 'manufacturer'},)
    partNumber = models.CharField(max_length=40, blank=True)


    class Meta:
        permissions = (
        ('modify_admin_site', 'Can modify, add, view, or delete manufacturer relationships'),
        )

view:


def PartView(request, type_id, id):
    partType = Type.objects.get(id=type_id)
    instance = get_object_or_404(Part, id=id)
    selection = None
    if request.method == 'POST':
        form = ViewPartForm(type_id, request.POST, request.FILES, instance=instance)
        manu1_formset = ManufacturerFormSet(request.POST, instance=instance)
        location1_formset = LocationFormSet(request.POST, instance=instance)
        if form.is_valid():
            selection = form.cleaned_data['active_feed']
            part = form.save(commit=False)
            part.partType_id = type_id
            if manu1_formset.is_valid() and location1_formset.is_valid():
                part.save()
                manu1_formset.save()
                location1_formset.save()
                url = reverse('list_parts', args=[partType.pk])
                return HttpResponseRedirect(url)
    else:
        form = ViewPartForm(type_id=type_id, instance=instance)
        manu1_formset = ManufacturerFormSet(instance=instance)
        location1_formset = LocationFormSet(instance=instance)
    return render(request, 'part_view.html', {'view_part_form': form,
                                              'location_formset': location1_formset,
                                              'manu_formset': manu1_formset,
                                              'selection': selection,
                                              'partType': partType,
                                              'part': instance})
class Manufacturer1Form(ReadOnlyFormMixin, ModelForm):
    manufacturer = forms.ModelChoiceField(queryset=Vendor.objects.filter(vendor_type='manufacturer').order_by('name'))

    class Meta:
        model = Manufacturer1Relationship
        exclude = ('part',)


    def __init__(self, *args, **kwargs):
        initial_manufacturer = kwargs.pop("manufacturer",None)
        super().__init__(*args, **kwargs)
        self.fields["manufacturer"].initial = initial_manufacturer




ManufacturerFormSet(request.POST, instance=instance, manufacturer=specific_manufacturer)

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