﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
33341	The PrimaryKey of an Unmanaged Model is incorrectly regognized (accessing database VIEW)	Janne	nobody	"I have used the following method earlier successfully with Django 3.0.6, but now there's a bug. 

But now the 3.2.9 catches a ""Gotcha"" in django/db/models/fields/related.py in ""get_instance_value_for_fields"" (line 662), I believe incorrectly.

Currently I have:

– A view on postgres (to access a managed model ""Rules"", with default id as PK): CREATE OR REPLACE  VIEW rules_view  

– Unmanaged model to access ""Rules"" data via ""rules_view"" view like this:

{{{
class LinkedRules(Rules)
    ....
    class Meta:
        managed = False
        db_table = 'rules_view

}}}


The problem: I am able to retrieve the data with, only with a raw SQL query, after changing the field name from id to rules_ptr_id  like this:


{{{
LinkedViewRules.objects.raw(f'SELECT id as rules_ptr_id , ...
}}}


Hope this helps. 

PS. Couldn't test my current project with old <3.2 version, due to other incompatibilities (with numpy array withespaces).
(Table names are paraphrased, hopefully without errors)


Hint from django/db/models/fields/related.py:

            # Gotcha: in some cases (like fixture loading) a model can have                                                                   
            # different values in parent_ptr_id and parent's id. So, use                                                                      
            # instance.pk (that is, parent_ptr_id) when asked for instance.id.                                                                
 "	Bug	closed	Uncategorized	3.2	Normal	needsinfo	Unmanaged model, Database VIEW, postgres		Unreviewed	0	0	0	0	0	0
