Fetching database Items based on Instance Settings

  django, django-views, instance, python

I have created a database model that saved user-added settings to the database. and I would like to read from these database entries to print reports.

I have already read from the forms instances to allow the use to edit the settings like this :

Url : path('accConnect/setting/<int:setting_pk>', views.viewSettings, name='viewSettings' ),

Views:

    setting = get_object_or_404(SettingsClass, pk=setting_pk)
    if request.method == 'GET':
        form = SettingUpdateForm(instance=setting)
        return render(request, 'main/viewSettings.html', {'setting': setting, 'form':form})
    else:
        form =  SettingUpdateForm(request.POST, instance=setting)
        if form.is_valid():
            form.save()
            return redirect('settingsHome')

However while trying to do the same thing to read from the class’ instace settings. I cannot seem to get the code right.

The below is what I have tried to get these instances but im not sure it works with the model

urls.py:

 #Reports
    path('reportsHome' , views.reportsHome, name='reportsHome'),
    path('accConnect/printReports/<int:reports_pk>' , views.printReports , name='printReports')

Views.py:

pkForm = get_object_or_404(SettingsClass , pk=reports_pk)
    form= SettingsClass(instance=pkForm)

This returns that the ‘pk’ is not found. So i’m not to sure what the proper way to do this is?

Source: Python Questions

LEAVE A COMMENT