Open a-p-f opened 1 day ago
Update:
My code seems to work as expected if I update the field-computing-method to return the primary key of the related object, instead of directly returning the related object. Ie:
@computed(
models.ForeignKey(Foo, models.CASCADE, related_name="+"),
depends=[("self", ["foo1"])],
)
def foo2(self):
return self.foo1.pk
This approach will work for me in current project, but I think my initial approach is the more natural/expected way. Thanks,
Alex
@a-p-f Thx for reporting. To be honest, ForeignKey
fields as CFs were never officially supported (and thus not tested), as they would screw up the dependency tree and make the resolvers life much harder. Yes they are reported to work for the first CF field, guess thats what you are doing.
Furthermore fk fields are treated differently at different stages by the ORM (at some point as DB-native value, at others as model instance), seems you just run into that difference here. There was a similar report in the past, imho the change happened around django ~4.x.
I agree, that returning the model type is more straight forward to comprehend, will see if I can find a workaround for that.
Hi there. I have a project that's been using django-computedfields for a while, but I've run into an error when upgrading Django and django-computedfields. It appears you can no longer have a computed ForeignKey field (which my project has been happily doing with django-computedfields==0.1.5).
models.py (Bar has a
computed
ForeignKey
to Foo):test code (just trying to save a Bar instance):
The test code runs fine with: Django==3.2.25 django-computedfields==0.1.5
But with: Django==5.1.3 django-computedfields==0.2.6
the
b.save()
call generates:PS: I had a quick look at
examples.test_full.models
, and it looks like you don't have any tests with a computed ForeignKey.