ForeignObject defaults to virtual_only=False?

55 views
Skip to first unread message

Tim Graham

unread,
Jul 14, 2015, 8:59:06 PM7/14/15
to django-d...@googlegroups.com
ForeignObject defaults to virtual_only=False [1]. This seems counterintuitive as my understanding is that ForeignObject fields are virtual (i.e. they don't directly map to a database column).

I am testing Andrew's patch to allow disabling migrations in tests [2] and ran into a problem in Django's test suite because of this. For models with ForeignObject fields [3], these fields appear in model._meta.local_fields and so are added to migrations which raises an exception because field.column is None. I tried changing ForeignObject to default to virtual_only=True (and kept ForeignKey virtual_only=False), but there were non-trivial test failures, so I wanted to check my understanding before going down a wormhole.

My experimental branch is https://github.com/django/django/pull/4997

[1] https://github.com/django/django/blob/b356dc4e07915521db1e768d6357e3d982877a6e/django/db/models/fields/related.py#L1830-L1832
[2] https://github.com/django/django/pull/4986
[3] https://github.com/django/django/blob/b356dc4e07915521db1e768d6357e3d982877a6e/tests/foreign_object/models.py#L60-L68

Anssi Kääriäinen

unread,
Jul 15, 2015, 3:58:29 AM7/15/15
to django-d...@googlegroups.com
If I recall correctly the real significance of virtual_only is that fields added with this flag are copied to child models. Generic relations need this as they need to know the model they are used on. Normal fields are not copied, instead they are fetched directly from the parent model when _meta is queried.

So, it might be possible to rename virtual_only to something like copy_to_childs, and then virtuality of a field would be determined only on it having a column.

Even better would be if fields had a get_migration_operations(from_state, to_state) method. Then migrations wouldn't have to know anything else than to call that method. This would also be very useful method for 3rd party fields.

 - Anssi
--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/e88f0124-ba52-4b21-8825-ae73824f1040%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages