Question

I extend default model manager and add cache-specific logic to it:

class ReadOnlyManager(manager.Manager):
    use_for_related_fields = True

    def create(self, **kwargs):
        obj = super(ReadOnlyManager, self).create(**kwargs)
        cache.cache_read_only_object(obj)
        ...
        return obj

    def update(self, *args, **kwargs):
        raise ReadOnlyException()

    def by_id(self, object_id):
        return cache.retrieve_read_only_object(self.model, object_id)

    def by_lookup(self, lookup_key, lookup_value):
        return cache.retrieve_read_only_object_by_lookup(self.model, lookup_key, lookup_value)

Then I created abstract model that uses it:

class ReadOnlyModel(models.Model):
    class Meta:
        abstract = True

    objects = ReadOnlyManager()

I use it in concrete model:

class TokenType(ReadOnlyModel):
    code = models.CharField(_('code'), max_length=30, unique=True)
    description = models.CharField(_('description'), max_length=100)

    lookups = {
        'code': 'code'
    }

When I tried to call method specific for custom cache, for example *by_id*:

TokenType.objects.by_id(1) # This code works

PyCharm highlights it and writes "Unresolved attribute reference" warning. When I press CMD+Space after TokenType., I see, that autocomplete box contains two objects items: one marked with function icon and have type ReadOnlyManager, second - with method icon and have type Manager.

Is it PyCharm bug? How to enable autocomlete for additional methods in custom manager?

Was it helpful?

Solution

This seems to be a problem of PyCharm. Writing an auto completion for Python is really a hard task, especially for things like Django Models, which uses Meta Classes and other nasty stuff.

However it is possible to complete, and it seems not so difficult, for your example my autocompletion ( https://github.com/davidhalter/jedi/tree/dev, work in progress, don't use it yet) is able to complete it:

Completing TokenType.objects. :

update                
by_id                 
by_lookup             
create                
use_for_related_fields

Completing TokenType.:

__metaclass__                 
__hash__                      
_get_next_or_previous_in_order
__ne__                        
date_error_message            
description                   
_perform_date_checks          
delete                        
clean                         
objects                       
unique_error_message          
_set_pk_val                   
_deferred                     
save_base                     
pk                            
serializable_value            
full_clean                    
__init__                      
code                          
save                          
__str__                       
validate_unique               
clean_fields                  
__repr__                      
_perform_unique_checks        
__reduce__                    
_get_unique_checks            
prepare_database_save         
_get_pk_val                   
__eq__                        
lookups                       
_get_next_or_previous_by_FIELD
Meta                          
_get_FIELD_display    

As far as I'm concerned, PyCharm is Closed Source, so I think you'll have to talk to the PyCharm developers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top