#29998: pk setup for MTI to parent get confused by multiple OneToOne references. -------------------------------------+------------------------------------- Reporter: Mārtiņš Šulcs | Owner: nobody Type: Bug | Status: new Component: Database layer | Version: 2.1 (models, ORM) | Severity: Normal | Resolution: Keywords: | Triage Stage: Accepted Has patch: 0 | Needs documentation: 0 Needs tests: 0 | Patch needs improvement: 0 Easy pickings: 0 | UI/UX: 0 -------------------------------------+------------------------------------- Changes (by Can Sarıgöl):
* cc: Can Sarıgöl (added) Comment: Hi, Could this approach be appropriate? {{{parent_links}}}'s params can be base and related class instance therefore just last sample columns are added into parent_links. We can extend key logic with related_name, e.g ('app', 'document', 'picking'). Or using this method, we can always ensure that the {{{parent_link=True}}} field is guaranteed into {{{self.parents}}}. {{{ +++ b/django/db/models/base.py @@ -196,10 +196,11 @@ class ModelBase(type): if base != new_class and not base._meta.abstract: continue # Locate OneToOneField instances. - for field in base._meta.local_fields: - if isinstance(field, OneToOneField): - related = resolve_relation(new_class, field.remote_field.model) - parent_links[make_model_tuple(related)] = field + fields = [field for field in base._meta.local_fields if isinstance(field, OneToOneField)] + for field in sorted(fields, key=lambda x: x.remote_field.parent_link, reverse=True): + related_key = make_model_tuple(resolve_relation(new_class, field.remote_field.model)) + if related_key not in parent_links: + parent_links[related_key] = field # Track fields inherited from base models. inherited_attributes = set() }}} -- Ticket URL: <https://code.djangoproject.com/ticket/29998#comment:5> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/065.47b8edf7ef3a0f4b8c3f95fc6db9df20%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.