类型对象'小部件'没有属性'_meta'

时间:2016-01-04 04:00:08

标签: django django-rest-framework django-filter

我正在尝试使用SerializerMethodField,我得到以下回溯 -

Traceback:
File "/opt/myproject_app/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  119.                 resolver_match = resolver.resolve(request.path_info)
File "/opt/myproject_app/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve
  365.             for pattern in self.url_patterns:
File "/opt/myproject_app/lib/python2.7/site-packages/django/core/urlresolvers.py" in url_patterns
  401.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/opt/myproject_app/lib/python2.7/site-packages/django/core/urlresolvers.py" in urlconf_module
  395.             self._urlconf_module = import_module(self.urlconf_name)
File "/usr/local/lib/python2.7/importlib/__init__.py" in import_module
  37.     __import__(name)
File "/opt/myproject/myproject/urls.py" in <module>
  28.     url(r'^api/', include('api.urls', namespace='api')),
File "/opt/myproject_app/lib/python2.7/site-packages/django/conf/urls/__init__.py" in include
  33.         urlconf_module = import_module(urlconf_module)
File "/usr/local/lib/python2.7/importlib/__init__.py" in import_module
  37.     __import__(name)
File "/opt/myproject/api/urls.py" in <module>
  4. from api import views
File "/opt/myproject/api/views.py" in <module>
  13. from api.filters import *
File "/opt/myproject/api/filters.py" in <module>
  25. class WidgetFilter(django_filters.FilterSet):
File "/opt/myproject_app/lib/python2.7/site-packages/django_filters/filterset.py" in __new__
  207.                                         new_class.filter_for_reverse_field)
File "/opt/myproject_app/lib/python2.7/site-packages/django_filters/filterset.py" in filters_for_model
  112.     opts = model._meta

Exception Type: AttributeError at /api/widgets/
Exception Value: type object 'Widget' has no attribute '_meta'

models.py

class Widget(models.Model):
    title = models.CharField(max_length=100)
    description = models.CharField(max_length=1024)
    username = models.CharField(max_length=50)
    code = models.CharField(max_length=1024)
    owner = models.ForeignKey('MyAppUser', related_name='MyAppUser_owner')
    list = models.ForeignKey('WidgetList')

serializers.py

class WidgetSerializer(serializers.ModelSerializer):
    owner = serializers.ReadOnlyField(source='owner.username')
    code = serializers.SerializerMethodField('get_code')
    list = serializers.CharField(source='list.name')

    class Meta:
        model=Widget
        fields = ('id', 'title', 'description', 'username', 'code', 'owner', 'list')

    def get_code(self, obj):
        code = decrypt_code(masterkey, userkey, obj.code)
        return code

filters.py

class WidgetFilter(django_filters.FilterSet):
    list = django_filters.CharFilter(name="list__name", lookup_type="exact")

    class Meta:
        model = Widget
        fields = ['list__name']

0 个答案:

没有答案