在不可调用的python3 django models.py中键入error str

时间:2019-10-06 11:46:16

标签: django python-3.x

我收到以下跟踪中所示的以下错误。我已经在我的项目中包含了models.py。

谢谢您的帮助!

from django.db import models
from django.db.models.fields.related import ForeignKey


class DeviceType(models.Model):
    """Model representing a device type."""
    type_device_name = models.CharField(max_length=100)
    short_name_device= models.CharField(max_length=7, default='Maszyna')
    who_is_made = models.CharField(max_length=100)
    additional_notes = models.TextField(max_length=1000, help_text="Type all important information about.")

    def __str__(self):
        """String for representing the Model object."""
        return f'{self.short_name_device}'

class Device(models.Model):
    """Model representing a device."""
    type_name = models.ManyToManyField(DeviceType, help_text='Select a device type')
    device_id = models.DecimalField(max_digits=4, decimal_places=0)
    short_name_device_own = ForeignKey(DeviceType, related_name='device_type_device', on_delete=models.SET_NULL, null=True)
    prod_year = models.DateTimeField(auto_now=False, auto_now_add=False)
    worked_science = models.DateField(auto_now=False)

    @property
    def __str__(self):
        """String for representing the Model object."""
        return f'{self.short_name_device_own}, {self.device_id},'



Traceback:

File "/home/robert/.local/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  34.             response = get_response(request)

File "/home/robert/.local/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  115.                 response = self.process_exception_by_middleware(e, request)

File "/home/robert/.local/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/robert/.local/lib/python3.6/site-packages/django/contrib/admin/options.py" in wrapper
  606.                 return self.admin_site.admin_view(view)(*args, **kwargs)

File "/home/robert/.local/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapped_view
  142.                     response = view_func(request, *args, **kwargs)

File "/home/robert/.local/lib/python3.6/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  44.         response = view_func(request, *args, **kwargs)

File "/home/robert/.local/lib/python3.6/site-packages/django/contrib/admin/sites.py" in inner
  223.             return view(request, *args, **kwargs)

File "/home/robert/.local/lib/python3.6/site-packages/django/contrib/admin/options.py" in add_view
  1634.         return self.changeform_view(request, None, form_url, extra_context)

File "/home/robert/.local/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapper
  45.         return bound_method(*args, **kwargs)

File "/home/robert/.local/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapped_view
  142.                     response = view_func(request, *args, **kwargs)

File "/home/robert/.local/lib/python3.6/site-packages/django/contrib/admin/options.py" in changeform_view
  1522.             return self._changeform_view(request, object_id, form_url, extra_context)

File "/home/robert/.local/lib/python3.6/site-packages/django/contrib/admin/options.py" in _changeform_view
  1565.                     self.log_addition(request, new_object, change_message)

File "/home/robert/.local/lib/python3.6/site-packages/django/contrib/admin/options.py" in log_addition
  810.             object_repr=str(object),

Exception Type: TypeError at /admin/dokumenty/device/add/
Exception Value: 'str' object is not callable

1 个答案:

答案 0 :(得分:1)

@property模型上的__str__方法中删除Device装饰器