Make img name unpredictable
This commit is contained in:
parent
641ace957b
commit
343e8b8834
2 changed files with 37 additions and 1 deletions
|
|
@ -0,0 +1,29 @@
|
|||
# Generated by Django 4.0.5 on 2022-07-04 21:26
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
import flangr.posts.models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("posts", "0001_initial"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name="comment",
|
||||
name="post",
|
||||
field=models.ForeignKey(
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name="comments",
|
||||
to="posts.post",
|
||||
),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name="post",
|
||||
name="img",
|
||||
field=models.ImageField(upload_to=flangr.posts.models.get_img_location),
|
||||
),
|
||||
]
|
||||
|
|
@ -1,3 +1,5 @@
|
|||
import uuid
|
||||
|
||||
from django.db import models
|
||||
|
||||
from django.conf import settings
|
||||
|
|
@ -5,8 +7,13 @@ from django.conf import settings
|
|||
# Create your models here.
|
||||
|
||||
|
||||
def get_img_location(instance, filename):
|
||||
uid = uuid.uuid4()
|
||||
return f"{uid[0:2]}/{uid[2:]}".replace("-", "")
|
||||
|
||||
|
||||
class Post(models.Model):
|
||||
img = models.ImageField(upload_to="posts/%Y/%m")
|
||||
img = models.ImageField(upload_to=get_img_location)
|
||||
posted = models.DateTimeField(auto_now_add=True)
|
||||
public = models.BooleanField(default=False)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue