fix: user can change avatar
This commit is contained in:
parent
c3c83b3168
commit
df436e0b88
@ -11,7 +11,7 @@ def upload_to(instance, filename: str):
|
|||||||
# Create your models here.
|
# Create your models here.
|
||||||
class ProfileModel(models.Model):
|
class ProfileModel(models.Model):
|
||||||
user = models.ForeignKey(User, on_delete=models.CASCADE)
|
user = models.ForeignKey(User, on_delete=models.CASCADE)
|
||||||
avatar_url = models.ImageField(upload_to=upload_to, default="../static/avatars/default.avif") #blank=True, null=True)
|
avatar_url = models.ImageField(upload_to=upload_to, default="./profiles/static/avatars/default.avif") #blank=True, null=True)
|
||||||
|
|
||||||
@receiver(post_save, sender=User)
|
@receiver(post_save, sender=User)
|
||||||
def on_user_created(sender, instance, created, **kwargs):
|
def on_user_created(sender, instance, created, **kwargs):
|
||||||
|
@ -38,7 +38,7 @@ class ProfileViewSet(viewsets.ModelViewSet):
|
|||||||
profile: ProfileModel = ProfileModel.objects.get(pk=self.request.user.pk)
|
profile: ProfileModel = ProfileModel.objects.get(pk=self.request.user.pk)
|
||||||
avatar = self.request.data.get("file", None)
|
avatar = self.request.data.get("file", None)
|
||||||
if (avatar is not None):
|
if (avatar is not None):
|
||||||
if (profile.avatar_url.name != "default.avif"):
|
if (profile.avatar_url.name != "./profiles/static/avatars/default.avif"):
|
||||||
profile.avatar_url.storage.delete(profile.avatar_url.name)
|
profile.avatar_url.storage.delete(profile.avatar_url.name)
|
||||||
profile.avatar_url = avatar
|
profile.avatar_url = avatar
|
||||||
profile.save()
|
profile.save()
|
Loading…
Reference in New Issue
Block a user