1

I have developed a website on Django. Initially, I used Django's default Database which is Sqlite3. Now I want to use Astra Datastax DB which is Cassandra. I am not able to convert Django.dB - models into Cassandra.cqlengine - columns function.

I have searched on the Internet and didn't find appropriate documents which could help me.

from django.db import models
from django.contrib.auth import get_user_model
from datetime import datetime
import uuid

User = get_user_model()

class Profile(models.Model):
    """docstring for Profile."""
    usr: str = models.ForeignKey(User, on_delete=models.CASCADE)
    id_usr: int = models.IntegerField()
    Fname:str = models.TextField(blank=True,null=True)
    Mname:str = models.TextField(blank=True,null=True)
    Lname:str = models.TextField(blank=True,null=True)
    Fhone:int = models.IntegerField(blank=True,null=True)
    bio: str = models.TextField(blank=True)
    img_profile = models.ImageField(
        upload_to='ProfileIMG', default="blankprofile.png")
    location: str = models.CharField(max_length=250)

    def __str__(self):
        return self.usr.username


class Post(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, unique=True)
    user: str = models.CharField(max_length=100)
    image = models.ImageField(upload_to="img_posts")
    caption: str = models.TextField(max_length=250)
    created_at = models.DateTimeField(default=datetime.now)
    Likes: int = models.IntegerField(default=0)

    def __str__(self):
        return self.user


class LikePost(models.Model):
    postid: str = models.CharField(max_length=100)
    username: str = models.CharField(max_length=100)

    def __str__(self):
        return self.username


class Followers(models.Model):
    follower: str = models.CharField(max_length=100)
    user: str = models.CharField(max_length=100)

    def __str__(self):
        return self.user

Specially, I want to convert this into Cassandra language.

img_profile = models.ImageField( upload_to='ProfileIMG', default="blankprofile.png")
prajod
  • 506
  • 5
  • 10

2 Answers2

1

The documentation you want to consult is here: https://docs.datastax.com/en/developer/python-driver/3.25/api/cassandra/cqlengine/columns/

These are the available columns for DjangoCassandraModel, which is what you would use instead of django.db.models to get a model backed by Cassandra. You can see a basic example of connecting to DataStax Astra with django_cassandra_engine here: https://github.com/DataStax-Examples/django-cassandra-blog

As for the ImageField, I am not sure of the Django internals here, but I believe it stores a path to the file in the database after putting it on disk at the location specified ("img_posts" in your example).

You could do the same for a Cassandra backed model, or use the Blob column type to store the image data itself. There are a number of articles and examples of doing this out there.

1

To add to the above answer, the django_cassandra_engine package does not offer anything with the same level of automation as the ImageField found in Django's models (i.e. storing the uploaded image on local disk and saving the string path to the database, all from the field definition in the model).

What you would do is to work at a slightly lower abstraction level with an explicit (Django, pure) form. This would allow you to manually handle the file upload as outlined here: https://docs.djangoproject.com/en/4.1/ref/forms/fields/#filefield . Once you have saved the file and have the string path to it, you can create ann istance of the corresponding Model and manually save it -- all in the appopriate view function.

As a side note, your original (sqlite-backed) code makes use of foreign keys and "on delete cascade" provision for removing related rows from other tables. This cannot be transported as is to a Cassandra storage, since the database, by itself, does not support the concept of relational integrity. You would have to decide how to handle these deletes and act on them explicitly in your code.

Stefano L
  • 36
  • 2