2021年2月5日星期五

django intra-model field dependancy

I'm using django Oscar and trying to extend the apps included models to add some fields from the django-imagekit library to a model. The issue is that my underlying app which controls the dashboards (oscar) populates the forms for the dashboard directly from the models using from django.forms.models import inlineformset_factory. I want the fields I'm adding to be based off one of the fields already in the model, so I don't want to change any forms.

Thus I need to intercept the input into the apps basic django.db.modelsImageField and use if for my new fields.

example of a fork, where I subclass the model I want to extend

    #code from app I'm forking      #link to src: https://github.com/django-oscar/django-oscar/blob/1fd9f0c3379042e1594f0d9281a5c323b5bafba2/src/oscar/apps/catalogue/abstract_models.py#L1302      class AbstractProductImage(models.Model):          """          An image of a product          """          product = models.ForeignKey(              'catalogue.Product',              on_delete=models.CASCADE,              related_name='images',              verbose_name=_("Product"))          original = models.ImageField(              _("Original"), upload_to=get_image_upload_path, max_length=255)          caption = models.CharField(_("Caption"), max_length=200, blank=True)  
    #my fork/subclass      from imagekit.models import ImageSpecField      class Product(AbstractProductImage):          srcset_image_1 = ImageSpecField(               source=self.origional.name,              processors=[ResizeToFill(100, 50)],              format=self.origional.name.split('.')[-1].upper(),              options={'quality': 80}          )  

Of course this throws an error:

NameError: name 'self' is not defined  

Not to mention there is a serious issue of how to keep the srcset_image_1 field from containing stale data. Is there a way to manage such an intra-model dependency where one fields values are calculated off of another?

https://stackoverflow.com/questions/66073214/django-intra-model-field-dependancy February 06, 2021 at 11:06AM

没有评论:

发表评论