Boris The Animal Boris The Animal - 2 months ago 15
Python Question

When filtering content of 2 models together getting - 'tuple' object has no attribute '_meta'?

I usually display filter options by one model.But now I have to allow to filter by the content of 2 models.

class Lease(CommonInfo):
version = IntegerVersionField( )
amount = models.DecimalField(max_digits=7, decimal_places=2)
is_notrenewed = models.BooleanField(default=False)
unit = models.ForeignKey(Unit)
is_terminated = models.BooleanField(default=False)


class LeaseConditions(CommonInfo):
version = IntegerVersionField( )
start_date = models.DateTimeField()
end_date = models.DateTimeField()
lease = models.ForeignKey(Lease)
increase = models.DecimalField(max_digits=7, decimal_places=2)
amount = models.DecimalField(max_digits=7, decimal_places=2)
is_terminated = models.BooleanField(default=False)
not_terminated_active_objects = NotTerminatedActiveManager()


So I added 2nd model to my filter

class LeaseFilter(django_filters.FilterSet):
class Meta:
model = Lease,LeaseConditions
fields = ['is_notrenewed', 'unit',
'is_terminated', 'start_date']


but getting error now


'tuple' object has no attribute '_meta'


What could be the problem?

Trace:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/lease/list/?is_notrenewed=1&unit=&is_terminated=1

Django Version: 1.8
Python Version: 2.7.11
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'concurrency',
'registration',
'crispy_forms',
'django_filters',
'common',
'client',
'lease',
'unit')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware')


Traceback:
File "C:\Users\Boris\dev\rentout\virtrentout\lib\site-packages\django\core\handlers\base.py" in get_response
119. resolver_match = resolver.resolve(request.path_info)
File "C:\Users\Boris\dev\rentout\virtrentout\lib\site-packages\django\core\urlresolvers.py" in resolve
366. for pattern in self.url_patterns:
File "C:\Users\Boris\dev\rentout\virtrentout\lib\site-packages\django\core\urlresolvers.py" in url_patterns
402. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\Boris\dev\rentout\virtrentout\lib\site-packages\django\core\urlresolvers.py" in urlconf_module
396. self._urlconf_module = import_module(self.urlconf_name)
File "c:\python27\Lib\importlib\__init__.py" in import_module
37. __import__(name)
File "C:\Users\Boris\dev\rentout\rentout\rentout\urls.py" in <module>
11. url(r'^lease/', include('lease.urls')),
File "C:\Users\Boris\dev\rentout\virtrentout\lib\site-packages\django\conf\urls\__init__.py" in include
33. urlconf_module = import_module(urlconf_module)
File "c:\python27\Lib\importlib\__init__.py" in import_module
37. __import__(name)
File "C:\Users\Boris\dev\rentout\rentout\lease\urls.py" in <module>
2. from lease import views
File "C:\Users\Boris\dev\rentout\rentout\lease\views.py" in <module>
59. class LeaseFilter(django_filters.FilterSet):
File "C:\Users\Boris\dev\rentout\virtrentout\lib\site-packages\django_filters\filterset.py" in __new__
181. filters = new_class.filters_for_model(opts.model, opts)
File "C:\Users\Boris\dev\rentout\virtrentout\lib\site-packages\django_filters\filterset.py" in filters_for_model
456. cls.filter_for_reverse_field
File "C:\Users\Boris\dev\rentout\virtrentout\lib\site-packages\django_filters\filterset.py" in filters_for_model
78. field = get_model_field(model, f)
File "C:\Users\Boris\dev\rentout\virtrentout\lib\site-packages\django_filters\utils.py" in get_model_field
72. opts = model._meta

Exception Type: AttributeError at /lease/list/
Exception Value: 'tuple' object has no attribute '_meta'

Answer

model property must be a model, not a tuple of models - you are specifying two models for an object that accepts only one. For filtering based on related objects, you can do it like this:

class LeaseFilter(django_filters.FilterSet):
    class Meta:
        model = LeaseConditions
        fields = ['lease__is_notrenewed', 'lease__unit', 'is_terminated', 'start_date']