Home django 2 crud operations without database with external api
Reply: 0

django 2 crud operations without database with external api

user4404 Published in May 21, 2018, 4:47 pm

I have an external third-party API client for retrieving and storing song names and I need to interface it with Django 2 and permit the user through Django forms to insert and update songs without using any database backend but only the API.

I'm struggling as most documentation on Django assumes a database backend and even examples on consuming an external API assume I want to store fetched data from a REST API to the database.

The external API is not even REST, I simply have a client.py module with all methods I need to do CRUD operations on a remote service storing song info.

I looked into creating a Django 2 custom model manager but it seems this is used as something above an existing database and not an external API. Django 2 serializers permit to serialize and deserialize data in between an API and a database.

What I'm missing is the way to create a custom model abstracting the API and permitting me to use it as if it were a database so that I can go on and follow any Django forms tutorial after having abstracted the model layer.

NOTE: similar problem asked here, answer was: "Django is a database-centric back-end framework, skip Django and use Angular" is that so hard for Django?

Currently I hacked something like the SSCE example below:

urls.py :

urlpatterns = [
    url(r'^$', viewSong, name='home'),
    url(r'^create/$', SongCreate.as_view(), name='create'),
    url(r'^update/(?P<song_id>[0-9\.a-z]+)/$', SongUpdate.as_view(), name='update'),

templates/form.html :

<form role="form" action="/create/" method="post">
                {% csrf_token %}
                <div class="form-group">
                    <label for="song-id">Song Id</label>
                    <input type="text" id="song-id" name="song-id">
                <div class="form-group">
                    <label for="song-name">Song Name</label>
                    <input type="text" id="song-name" name="song-name">

views.py :

class SongCreate(CreateView):
    template_name = 'form.html'

    def get(self, request, *args, **kwargs):
        form = SongForm()
        return render(request, self.template_name, {'form': form})

    def post(self, request):
        form = SongForm(request.POST)
        if form.is_valid():
            return redirect("/")
        return render(request, self.template_name, {'form': form})

class SongUpdate(UpdateView):
    template_name = 'form.html'

    def get(self, request, *args, **kwargs):
        song_id = kwargs["song_id"]
        obj = SongModel.load(song_id)
        form = SongForm(obj)
        return render(request, self.template_name, {'form': form})

forms.py :

class SongForm(forms.Form):
    song_id = forms.CharField(label='song_id', max_length=30)
    song_name = forms.CharField(label='song_name', max_length=100)

    def save(self):
        data = self.cleaned_data
        songModel = SongModel(song_id=data['song_id'],

models.py :

from django.db import models
from woozy.song_client import SongClient

username = 'woozyuser'
password = 'userpass'
base_url = 'http://localhost:8080'

class SongModel(models.Model):
    song_id = models.CharField(max_length=80)
    song_name = models.CharField(max_length=80)

    def save(self, force_insert=False, force_update=False, using=None,
        songClient = SongClient()
        songClient.connect(base_url, username, password)

    def load(cls, song_id):
        song_model = cls(song_id=song_id)
        songClient = SongClient()
        songClient.connect(base_url, username, password)
        song = songClient.get_song(song_id)
        return song

the above seems a little convoluted to me so I guess I'm not respecting the various layers and architecture that Django supports.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.305198 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO