0

I built an application that uses REST apis to inject information into a huge already existing database for a company. The application is a web form that the user fills out. My application then serializes the user's responses into a json that it uses to send post requests to the existent db.

My Django app also is connected to a SQL Server db where it is saving the responses of the user into the fields that I created in my models.py.

Is there a better way to do this? It seems like I'm saving all the information twice! A waste of space.

6
  • You can use django forms without saving the data to django database.Just get form post data in a view and send the data directly to your api. Commented Mar 22, 2020 at 16:22
  • @LearnToday, do you have any thoughts on the need for a db for contenttypes and session? I am taking care of auth a different way and don't need admin. Commented Mar 26, 2020 at 8:32
  • contenttypes is useful for Django Models. Each model represents its own content-type. If you are not using Django models, meaning you are not using the Django database, then there's no need for any of them. I am not sure of how you are taking care of your auth, if you are not using Django Auth, how are u able to get a user auth session? And another question is, why do u need django for your application? It seems u don't need any of it's core functionalities. Commented Mar 26, 2020 at 8:50
  • I'm using Auth0 for authentication. It makes it's own tables using Django social-auth in a db. But I want that auth db to be a shared db with other applications. Commented Mar 26, 2020 at 9:30
  • It could be I could implement this application in a different way but basically I have multiple Django apps. I want all of them to share an auth db and then each have another db for things related to that specific application. This specific one that my question was about doesn't really need anything in a db unless it's related to auth Commented Mar 26, 2020 at 9:31

1 Answer 1

1

I don't think you need anything in your models.py for this particular application. Personally I like the approach of letting the Form handle the saving process. You could do something like this:

import json
from django import forms

class MyForm(forms.Form):
    field1 = forms.CharField()
    field2 = forms.IntegerField()
    ...

    def save(self):
        json_data = json.dumps(self.cleaned_data)
        # insert code here to send the data to your REST API

Your view can then simply call the form.save() method in your view, even though this form is not a ModelForm.

if request.POST:
    form = MyForm(request.POST)
    if form.is_valid():
        form.save()
        return redirect(success_url)
return render(request, 'form.html', {'form': form})
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I see in my db that I have currently that I have the tables django_admin_log, django_content_type, django_migrations and django_sessions. If I do it the way you're suggesting, none of those are necessary? Can I delete the entire backend database or it still needs to be there?
The database is needed for the auth, admin, contenttypes and session apps. It's cumbersome to run without a database, although you can

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.