Reputation: 1572
I want to generate a value for name
field based on the file
field.
class Files(models.Model):
name = models.CharField(max_length=100, blank=True, null=True)
file = models.FileField(
upload_to=settings.RAW_IMAGE_UPLOAD_PATH,
null=True,
max_length=500
)
def name(self):
return os.path.basename(self.file.name)
def __str__(self):
return self.name
I am not successful when doing some Serialization/Deserialization.
Upvotes: 0
Views: 35
Reputation: 886
if you want to add a name for the file being uploaded.
class Files(models.Model):
name = models.CharField(max_length=100, blank=True, null=True)
file = models.FileField(
upload_to=settings.RAW_IMAGE_UPLOAD_PATH,
null=True,
max_length=500
)
def save(self, *args, **kwargs):
if self.name is None or self.name == '':
self.name = os.path.basename(self.file)
super().save(*args, **kwargs)
Upvotes: 1