Hi Maninder,

This error seems because i used choice field which actually use names as 
values but django expects the id.

As Django automatically creates a ModelChoicefield for a Foreign Key which 
use the correct values,i modified the code as below in forms.py.

I have a requirement to populate only unique values for registered_model 
from the values returned from ModelRegister .

I modified the code as below and now i am able to get the unique values .

====================================================================================================================
def unique_values():
    return 
[("","---------------")]+list(ModelRegister.objects.values_list('model_name','model_name').order_by('model_name').distinct())

registered_model = forms.ChoiceField(required=False,choices=unique_values)
====================================================================================================================

registered_model = 
forms.ModelChoiceField(queryset=ModelRegister.objects.values_list('model_name',flat=True).order_by('model_name').distinct())

====================================================================================================================

Error is resolved now.



   But once i fill all the values in formset and submit the form. 
   
   Not able to redirect to List View. Even if i click on submit button it 
   stays on the same page.Anything needs to be taken care during POST request 
   to save the created model formsets in the database.in the
   - 
   
   
   - 
   
   *def create_model_gsp_request_form(request,id=None):
       print("Inside create_model_gsp_request_form")
       template_name = "ModelGSPRequest.html"
       heading_message = "ModelGSPRequest"
   
       if id:
           action = 'edit'
           model = get_object_or_404(ModelGSPRequest, pk=id)
       else:
           action = 'submit'
           model = ModelGSPRequest()
   
       message = ""
   
       if request.method == 'GET':
           print("Get request in create_model_gsp_request_form")
           # we don't want to display the already saved model instances
           #formset = 
ModelGSPRequestFormSet(queryset=ModelGSPRequest.objects.none(),instance=model)
           formset = ModelGSPRequestFormSet()
           #registered_model_details = serializers.serialize("json", 
ModelRegister.objects.all())
           #print("registered_model_details:", registered_model_details)
       elif request.method == 'POST':
           print("post request in create_model_gsp_request_form")
           formset = ModelGSPRequestFormSet(request.POST,request.FILES)
           if formset.is_valid():
               for form in formset:
                   # only save if name is present
                   #if form.cleaned_data.get('name'):
   
                       new_form = form.save(commit=False)
                       new_form.model_gsp_requester = request.user.username
                       new_form.save()
                       formset.save_m2m()
               #return redirect('ModelGSPRequestList')
               return HttpResponseRedirect('/gspapp/ModelGSPRequestList')
       return render(request, template_name, {
               'formset': formset,
               'heading': heading_message,
               #'registered_model_data': registered_model_details,
               'action':action
           })*
   
   - 
   
   
   

Regards,
N.Dilip Kumar.


On Monday, February 10, 2020 at 12:11:42 AM UTC+5:30, Dilipkumar Noone 
wrote:
>
>  I am a Django learner. 
>
> 1.I have a requirement to create the multiple forms in a single page for a 
> model. 
> 2.So i created the formset using modelformset_factory. 
> 3.I have 2 models.
> a)ModelRegister b)ModelGSPRequest. 4.ModelGSPRequest has a field named 
> registered_model which is a foreignKey to ModelRegister. 5.But after i 
> input all the fields for a modelformset and click on submit to save. I am 
> facing below error with the foreign key field.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/9d67053f-c0e1-4f68-b875-7886b62ea5fd%40googlegroups.com.

Reply via email to