Cascading ondelete of session.

11 views
Skip to first unread message

Eliot Simcoe

unread,
Mar 22, 2018, 11:12:41 PM3/22/18
to Django users
Hello world,

I'm very new to Django and I come from a web2py background, so if this should be obvious please forgive me. My webapp allows anonymous users to upload files to my server for processing. I would like to track these files by the session, and when the session expires I would like these files to be deleted. I understand that I can run the manage.py clearsessions command to remove expired sessions, but I'm a little fuzzy on how to make this connection. I have added the django-cleanup application to my webapp and I believe that it is installed correctly - though I'm not sure how to test this. I currently have a table similar to the following:

class Raster(models.Model):
    image
= models.ImageField(
        upload_to
='rasters', width_field='width', height_field='height')
    width
= models.IntegerField(validators=[MinValueValidator(0)])
    height
= models.IntegerField(validators=[MinValueValidator(0)])
   
   
# when the session is deleted, this raster should be deleted as well
    session
= models.ForeignKey(Session, on_delete=models.CASCADE)

I'm able to populate the table with the an image file and session relation using the following class method of Raster:

    @classmethod
   
def Create_With_File(cls, name, file, session=None):
       
# build the raster
        raster
= cls(
            session_id
= session.session_key
       
)
       
       
# store the image file
        raster
.image.save(path.basename(name), ImageFile(file))

       
return raster
   

This is all well and good - my file is created in the filesystem and the record is added in the database AFAIK. However, when I run manage.py clearsessions my files persist. I guess I have a couple of related questions:

1. I don't see a Session table in the admin interface, so it is difficult for me to tell what is working and what is not... Should I be able to inspect a Session table through admin?? For that matter I don't see a Raster table to inspect either...

2. Assuming that sessions are being stored and removed from the database correctly am I incorrect to assume that the manage.py clearsessions command would trigger the ondelete cascade as defined in my model?

Thanks for any help you can give. I'm a little lost.
Reply all
Reply to author
Forward
0 new messages