I have following inheritance between Image and ProfileImage & ThumbnailStaticImage classes in Django :
class Image(models.Model):
uuid = models.CharField(max_length=12, default="")
extension = models.CharField(max_length=6, default=None)
filename = models.CharField(max_length=20, default=None)
path = models.CharField(max_length=64, default=None)
class Meta:
abstract = True
def save(self, *args, **kwargs):
if self.uuid is None:
self.uuid = "T" + get_random_string(11).lower()
super(Image, self).save(*args, **kwargs)
def delete(self, *args, **kwargs):
delete_message_send(self.path)
super(Image, self).delete(*args, **kwargs)
class ProfileImage(Image):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
def save(self, *args, **kwargs):
if self.extension is None:
self.extension = ".png"
if self.filename is None:
self.filename = self.uuid + self.extension
if self.path is None:
self.path = self.user.path + "/" + self.filename
super(ProfileImage, self).save(*args, **kwargs)
class ThumbnailStaticImage(Image):
video = models.ForeignKey(Video, on_delete=models.CASCADE, default=None)
def save(self, *args, **kwargs):
if self.extension is None:
self.extension = ".png"
if self.filename is None:
self.filename = self.uuid + self.extension
if self.path is None:
self.path = settings.STORAGE.THUMBNAILDIRECTORY + "/" + self.filename
super(ThumbnailStaticImage, self).save(*args, **kwargs)
When I try to access the extension variable that should be inherited from Image class to ProfileImage class, it does not get that information from parent class.
class CustomProfileImageSignedURL(APIView):
@method_decorator(login_required(login_url='/login/'))
def post(self, request):
profileimage = ProfileImage.objects.
create(user=request.user)
signed_url = get_signed_url_for_upload(
path=profileimage.path,
content_type='image/png')
logging.debug(signed_url);
return Response({
"custom_profile_image_signed_url":signed_url,
"image_uuid":profileimage.uuid})
What is the purpose of inheritance if the child class does not get the fields that parent class has directly?
How can I access the parent class fields from child class' overriding save method ?
savemethod does not save the object, you need to call thesuper().save()to eventually save the item to the database.ProfileImages andThumbnailStaticImages? With some view, with form? Can you share details about this?uuidto have a value in theProfileImage.savemethod?uuidis set in theImage.savemethod which is called at the end of theProfileImage.savemethod when you call super