Closed tobych closed 11 years ago
Hi, thanks for reporting!
This patch sounds good for me. Do you create some automated test that reproduce this error to include in the pull request?
If you did not create, I will create for you.
Thanks again.
I didn't write a test actually. I think it was the day Github was down for a while and I couldn't do any work! Let me know if you'd still like me to do that.
Hi Toby.
I think with more attention about this. I believe this is an anti-pattern, since this will incentivate you to use a magic number in your models, and worst, a magic ID. Of course you can give a name to this constant, but I believe it is a better idea to create a method for this, so it will be possible to handle errors in a more properly way. Check the following example (I added an automated test for this scenario and it is working without any change in the code).
def default_fk_value():
try:
return ModelRelated.objects.get(id=1)
except ModelRelated.DoesNotExist:
return None
foreignkey_with_default = models.ForeignKey('ModelRelated', related_name='fk2', null=True, default=default_fk_value)
def test_new_deal_with_default_values(self):
instance = self.ddf.new(ModelWithRelationships)
self.assertTrue(isinstance(instance.foreignkey_with_default, ModelRelated))
What do you think? Do you agree?
Regards
Hi Paulo. I'm coming back to this rather late in the day, and only because of the subsequent issue you're referencing above. I do see what you're doing above, but my model is as I write, and I'm surprised you want to get involved in recommending certain approaches to modeling over others, I guess, and I know the foreign row will be there, so I don't need this extra code, and wouldn't like to have to change my model even this much to work around a limitation in DDF. Right now I'm using my fork of DDF.
I will include this patch in DDF!
I'm using f5784b, and I've a model with this field:
When I try to instantiate this using G, I get:
I've not looked super long at the code, but I'll submit a pull request for code that's got it working for me.
Toby