netzkolchose / django-computedfields

Provides autogenerated autoupdated database fields for model methods.
MIT License
94 stars 14 forks source link

One to many relationship , resolver exception when resolving dependency on related set #143

Open sbatdkio opened 9 months ago

sbatdkio commented 9 months ago

Hi, maybe i did'nt understand the way to handle this, however this is my case: I have a Campaign/Action_Model one to many relationship & i want a "is_finished" field to be computed in the campaign from the related Action_Models

class  Campaign(ComputedFieldsModel):
  ...
  is_finished = ComputedField(
        BooleanField(default=False),
        depends=[("action_model_set", ["campaign_fk"])],
        compute=is_camapaign_finished,
    )

in another file , i am defining the Action_Model class

class Action_Model(models.Model):
    name = models.CharField(max_length=200, db_index=True)
    campaign_fk = models.ForeignKey(Campaign, on_delete=models.CASCADE)
    ...
    startDate = models.DateField(db_index=True)
    endDate = models.DateField(db_index=True)
   ...

The computedfields resolver says the action_model_set in the Campaign model does not exist as field. Here is the exception that is raised: File ".../site-packages/computedfields/graph.py", line 558, in resolve_dependencies descriptor = getattr(cls, symbol) AttributeError: type object 'Campaign' has no attribute 'action_model_set'

Could you tell me what i did wrong ?

jerch commented 9 months ago

@sbatdkio Hmm, it works with your provided info here. Does that other file, where Action_Model is defined, reside in a different django app?

This pretty much looks like a model resolution issue - can you access action_model_set on Campaign in the shell, if you comment out the computed field temporarily? I'd guess, thats also not possible, maybe got named differently. Alternatively try using an explicit related name.