[Django] #11448: Defining relationships after querying a model does not add a reverse lookup to the referenced model

View: New views
9 Messages — Rating Filter:   Alert me  

[Django] #11448: Defining relationships after querying a model does not add a reverse lookup to the referenced model

by Django-3 :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

#11448: Defining relationships after querying a model does not add a reverse lookup
to the referenced model
------------------------------------------+---------------------------------
 Reporter:  Dennis Kaarsemaker            |       Owner:  nobody    
   Status:  new                           |   Milestone:            
Component:  Database layer (models, ORM)  |     Version:  SVN      
 Keywords:                                |       Stage:  Unreviewed
Has_patch:  0                             |  
------------------------------------------+---------------------------------
 Ok, that sounds vague but I don't know how to better describe it.
 Basically it boils down to having this in models.py:

 {{{
 from django.db import models
 import os

 class c1(models.Model):
     name = models.CharField("Name", max_length=30)

 default_c1 = c1.objects.get(name="non_existant")

 class c2(models.Model):
     other = models.ForeignKey(c1, default=default_c1)
 }}}

 Querying c1 later with c1.objects.filter(c2__pk=0) will fail:
 FieldError: Cannot resolve keyword 'c2' into field. Choices are: id, name

 Minimal testcase project attached (models.py is slightly bigger than
 above). You can reproduce the problem with:

 {{{
 # Create the database (clobbers test.db in the current dir)
 ./manage.py syncdb
 # See that without querying in between it works
 echo -e "from proj1.app1.models import c1\nc1.objects.filter(c2__pk=0)" |
 ./manage.py shell
 # See that with querying in between it fails
 echo -e "from proj1.app1.models import c1\nc1.objects.filter(c2__pk=0)" |
 BREAK_ME=1 ./manage.py shell
 }}}

 Found on 1.0.2, confirmed with trunk (fresh checkout, less than 30 minutes
 ago)

--
Ticket URL: <http://code.djangoproject.com/ticket/11448>
Django <http://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 post to this group, send email to django-updates@...
To unsubscribe from this group, send email to django-updates+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: [Django] #11448: Defining relationships after querying a model does not add a reverse lookup to the referenced model

by Django-3 :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

#11448: Defining relationships after querying a model does not add a reverse lookup
to the referenced model
---------------------------------------------------+------------------------
          Reporter:  Dennis Kaarsemaker            |         Owner:  nobody
            Status:  new                           |     Milestone:        
         Component:  Database layer (models, ORM)  |       Version:  SVN  
        Resolution:                                |      Keywords:        
             Stage:  Unreviewed                    |     Has_patch:  0    
        Needs_docs:  0                             |   Needs_tests:  0    
Needs_better_patch:  0                             |  
---------------------------------------------------+------------------------
Changes (by Dennis Kaarsemaker):

  * needs_better_patch:  => 0
  * needs_tests:  => 0
  * needs_docs:  => 0

Comment:

 Manually deleting the _related_objects_cache (and for good measure the
 _related_many_to_many_cache) works around the problem. I'd rather see
 django do that when defining a new model. Patch to follow soon.

--
Ticket URL: <http://code.djangoproject.com/ticket/11448#comment:1>
Django <http://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 post to this group, send email to django-updates@...
To unsubscribe from this group, send email to django-updates+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: [Django] #11448: Defining relationships after querying a model does not add a reverse lookup to the referenced model

by Django-3 :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

#11448: Defining relationships after querying a model does not add a reverse lookup
to the referenced model
---------------------------------------------------+------------------------
          Reporter:  Dennis Kaarsemaker            |         Owner:  nobody
            Status:  new                           |     Milestone:        
         Component:  Database layer (models, ORM)  |       Version:  SVN  
        Resolution:                                |      Keywords:        
             Stage:  Unreviewed                    |     Has_patch:  1    
        Needs_docs:  0                             |   Needs_tests:  0    
Needs_better_patch:  0                             |  
---------------------------------------------------+------------------------
Changes (by Dennis Kaarsemaker):

  * has_patch:  0 => 1

Comment:

 Attached patch clears the relevant _related_*_cache in
 contribute_to_related_class of OneToOneField, ManyToManyField and
 ForeignKey. This makes new relationships visible even if a query that
 triggers filling this cache has been executed beforehand. Tested against
 the test project (and another, proprietary, one).

--
Ticket URL: <http://code.djangoproject.com/ticket/11448#comment:2>
Django <http://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 post to this group, send email to django-updates@...
To unsubscribe from this group, send email to django-updates+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: [Django] #11448: Defining relationships after querying a model does not add a reverse lookup to the referenced model

by Django-3 :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

#11448: Defining relationships after querying a model does not add a reverse lookup
to the referenced model
---------------------------------------------------+------------------------
          Reporter:  Dennis Kaarsemaker            |         Owner:  nobody
            Status:  new                           |     Milestone:        
         Component:  Database layer (models, ORM)  |       Version:  SVN  
        Resolution:                                |      Keywords:        
             Stage:  Accepted                      |     Has_patch:  1    
        Needs_docs:  0                             |   Needs_tests:  1    
Needs_better_patch:  0                             |  
---------------------------------------------------+------------------------
Changes (by Alex):

  * needs_tests:  0 => 1
  * stage:  Unreviewed => Accepted

Comment:

 Couple things:
 1. del is a statement, so no need for the parentheses
 2. I think the patch reads a little better as a hasattr() test instead of
 catching the exception.  Also put a comment next to each of these saying
 if the cache is populated we clear it out because it needs to be
 repopulated to include the attr we're about to assign.
 3. Can you put a testcase in the Django tests that demonstrates that this
 has been fixed.

 Otherwise the patch looks good to me.

--
Ticket URL: <http://code.djangoproject.com/ticket/11448#comment:3>
Django <http://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 post to this group, send email to django-updates@...
To unsubscribe from this group, send email to django-updates+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: [Django] #11448: Defining relationships after querying a model does not add a reverse lookup to the referenced model

by Django-3 :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

#11448: Defining relationships after querying a model does not add a reverse lookup
to the referenced model
---------------------------------------------------+------------------------
          Reporter:  Dennis Kaarsemaker            |         Owner:  nobody
            Status:  new                           |     Milestone:        
         Component:  Database layer (models, ORM)  |       Version:  SVN  
        Resolution:                                |      Keywords:        
             Stage:  Accepted                      |     Has_patch:  1    
        Needs_docs:  0                             |   Needs_tests:  1    
Needs_better_patch:  0                             |  
---------------------------------------------------+------------------------
Comment (by Dennis Kaarsemaker):

 1. That's my coding style slipping though, will fix
 2. I followed the style in get_all_related_objects_with_model, but agree
 that a hasattr() reads better.
 3. I'm very unfamiliar with django's test setup. Can you point me to some
 documentation?

--
Ticket URL: <http://code.djangoproject.com/ticket/11448#comment:4>
Django <http://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 post to this group, send email to django-updates@...
To unsubscribe from this group, send email to django-updates+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: [Django] #11448: Defining relationships after querying a model does not add a reverse lookup to the referenced model

by Django-3 :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

#11448: Defining relationships after querying a model does not add a reverse lookup
to the referenced model
---------------------------------------------------+------------------------
          Reporter:  Dennis Kaarsemaker            |         Owner:  nobody
            Status:  new                           |     Milestone:        
         Component:  Database layer (models, ORM)  |       Version:  SVN  
        Resolution:                                |      Keywords:        
             Stage:  Accepted                      |     Has_patch:  1    
        Needs_docs:  0                             |   Needs_tests:  1    
Needs_better_patch:  0                             |  
---------------------------------------------------+------------------------
Comment (by Alex):

 1) Yeah, I understand, but we try to follow PEP8 where possible.

 3) Here are the docs on Django's test framework:
 http://docs.djangoproject.com/en/dev/topics/testing/?from=olddocs#writing-
 tests, here's some info on getting setup to run the test suite:
 http://lazypython.blogspot.com/2008/11/running-django-test-suite.html,
 lastly take a look at the tests/regressiontests directory of the source.
 Each directory in there is a set of self contained tests.  However, the
 nature of this problem makes me think it will be very difficult to tests,
 so if it seems impossible I wouldn't waste a ton of time on it.

--
Ticket URL: <http://code.djangoproject.com/ticket/11448#comment:5>
Django <http://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 post to this group, send email to django-updates@...
To unsubscribe from this group, send email to django-updates+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: [Django] #11448: Defining relationships after querying a model does not add a reverse lookup to the referenced model

by Django-3 :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

#11448: Defining relationships after querying a model does not add a reverse lookup
to the referenced model
---------------------------------------------------+------------------------
          Reporter:  Dennis Kaarsemaker            |         Owner:  nobody
            Status:  new                           |     Milestone:        
         Component:  Database layer (models, ORM)  |       Version:  SVN  
        Resolution:                                |      Keywords:        
             Stage:  Accepted                      |     Has_patch:  1    
        Needs_docs:  0                             |   Needs_tests:  1    
Needs_better_patch:  0                             |  
---------------------------------------------------+------------------------
Comment (by Dennis Kaarsemaker):

 Updated patch: fixing del(), adding comments and adding a test case.
 ./runtests.py -v2 query_between_definitions fails without the (rest of
 the) patch applied and succeeds otherwise. I cheated a little bit by not
 actually running a query, but the calling init_name_map(). This is the bit
 that actually causes the problem and running a query would call this
 function too.

--
Ticket URL: <http://code.djangoproject.com/ticket/11448#comment:6>
Django <http://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 post to this group, send email to django-updates@...
To unsubscribe from this group, send email to django-updates+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: [Django] #11448: Defining relationships after querying a model does not add a reverse lookup to the referenced model

by Django-3 :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

#11448: Defining relationships after querying a model does not add a reverse lookup
to the referenced model
---------------------------------------------------+------------------------
          Reporter:  Dennis Kaarsemaker            |         Owner:  nobody
            Status:  new                           |     Milestone:        
         Component:  Database layer (models, ORM)  |       Version:  SVN  
        Resolution:                                |      Keywords:        
             Stage:  Accepted                      |     Has_patch:  1    
        Needs_docs:  0                             |   Needs_tests:  0    
Needs_better_patch:  0                             |  
---------------------------------------------------+------------------------
Changes (by seveas):

  * needs_tests:  1 => 0

--
Ticket URL: <http://code.djangoproject.com/ticket/11448#comment:7>
Django <http://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 post to this group, send email to django-updates@...
To unsubscribe from this group, send email to django-updates+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: [Django] #11448: Defining relationships after querying a model does not add a reverse lookup to the referenced model

by Django-3 :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

#11448: Defining relationships after querying a model does not add a reverse lookup
to the referenced model
---------------------------------------------------+------------------------
          Reporter:  Dennis Kaarsemaker            |         Owner:  nobody
            Status:  new                           |     Milestone:        
         Component:  Database layer (models, ORM)  |       Version:  SVN  
        Resolution:                                |      Keywords:        
             Stage:  Accepted                      |     Has_patch:  1    
        Needs_docs:  0                             |   Needs_tests:  0    
Needs_better_patch:  0                             |  
---------------------------------------------------+------------------------
Comment (by seveas):

 #11247 is a different manifestation of this bug, which is more likely to
 occur.

--
Ticket URL: <http://code.djangoproject.com/ticket/11448#comment:8>
Django <http://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 post to this group, send email to django-updates@...
To unsubscribe from this group, send email to django-updates+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---