Here’s a quick start guide for using the Viewflow library with Django, assuming that the Viewflow library is already configured in your Django settings:
Begin by creating a new Django app if you haven’t already. Use the Django startapp command to do this. For example, if you want to create an app named ‘atlas’, run:
python manage.py startapp atlas
Define a new model in your app. In this example, we’ll create a City model. Add the following code to the models.py file in your app:
from django.db import models
class City(models.Model):
name = models.CharField(max_length=250)
population = models.PositiveIntegerField()
``` This model includes two fields: name for the city name and population for its population.
You will need to create a viewset for your model. This can be done by using ModelViewset from Viewflow. Add the following code to a new file (or an existing one if you prefer) in your app:
from viewflow.urls import ModelViewset
from . import models
class CityViewset(ModelViewset):
model = models.City
``` Here, CityViewset is a class based on ModelViewset that is linked to the City model.
Finally, link your viewset to a URL by editing the urls.py file in your Django project. Add the following code to the urls.py file:
from django.contrib import admin
from django.urls import path
from atlas.viewsets import CityViewset
urlpatterns = [
path("", CityViewset().urls),
path("admin/", admin.site.urls),
]
This code sets up a URL pattern for your viewset and the Django admin site.
By following these steps, you’ve set up a basic structure using the Viewflow library in your Django project. Remember to run migrations for your new model using python manage.py makemigrations and python manage.py migrate. Additionally, you might want to customize your viewset further based on the requirements of your project.