I am creating property methods for every model where the model attribute includes ImageField or FileField. So, I decided to make an abstract model where I check the fields in the model and if there are any ImageField and FileField in the model the property method creates it automatically by itself.
I usually add '_url' to the attribute when I create the method
Below is what I do usually
class MyModel(models.Model):
image = ImageField(...)
file = FileField(...)
...
@property
def image_url(self):
if self.image and hasattr(self.image, 'url'):
return self.image.url
@property
def file_url(self):
if self.file and hasattr(self.file, 'url'):
return self.file.url
...
Below what I did so far
class MyModel(models.Model):
...
def __new__(cls, value):
fields = self._meta.get_fields()
for field in fields:
if isinstance(field, ImageField) or isinstance(field, FileField):
???
Any suggestions?
CodePudding user response:
Use mixins.
class ImageUrlMixin:
@property
def image_url(self):
if self.image and hasattr(self.image, "url"):
return self.image.url
class FileUrlMixin:
@property
def file_url(self):
if self.file and hasattr(self.file, "url"):
return self.file.url
class FileImageUrlMixin(FileUrlMixin, ImageUrlMixin):
pass
class OnlyHasFileFieldModel(FileUrlMixin, models.Model):
# ..model implementation
class OnlyHasImageFieldModel(ImageUrlMixin, models.Model):
# ..model implementation
class HasBothFileAndImageFieldModel(FileImageUrlMixin, models.Model):
# ..model implementation
Or if you want to support fields dynamically e.g. my_model.arbitrary_field_url
:
class DynamicFieldUrlMixin:
def __getattr__(self, name):
if name.endswith("_url"):
field_name = "".join(name.split("_")[:-1])
field = getattr(self, field_name, None)
if hasattr(field, "url"):
return field.url
raise AttributeError