Django Admin“ TypeError int()参数必须是字符串,类似字节的对象或数字,而不是'Car'”

时间:2019-10-08 05:48:13

标签: django python-3.x admin

我的项目Admin工作正常,但是由于我一直在努力将图像链接到我的模型对象,因此引发了该错误。在处理模型时,特定的错误代码很常见,但我见过的具体的管理站点都没有。

到目前为止,我尝试使用新类进行新的迁移,效果很好,并且还删除了一些管理字段。

models.py汽车模型 我跳过了很多东西,但是自从上次工作以来,我什么都没有改变。

class Car(models.Model):
    manufacturer = models.ForeignKey('Manufacturer', on_delete=models.SET_NULL, null=True)
    car_model = models.CharField('Model', max_length=50, null=True)
    description = models.TextField(max_length=4000)
    vin = models.CharField('VIN', max_length=17, help_text='Enter the 17 character VIN number.', blank=True, null=True)
    mileage = models.IntegerField(verbose_name='Mileage')
    car_images = models.ImageField(help_text='Upload pictures', upload_to=image_directory_path, storage=image_storage)
    date_added = models.DateTimeField(auto_now_add=True)
    engine_displacement = models.CharField(default=2.0, max_length=3, help_text="Engine displacement in Liters (E.g. 2.0, 4.2, 6.3)")
    price = models.IntegerField(default=0)

    seller = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)

    id = models.UUIDField(primary_key=True, default=uuid.uuid4,
    help_text="Unique ID for this car")
...
    class Meta:
        ordering = ['date_added']
        permissions = (("can_change_availability", "Mark car as sold"),)  

    def __str__(self):
        return f'{self.manufacturer} {self.car_model}'

    def get_absolute_url(self):
        return reverse('showroom:car-detail', args=[str(self.pk)])

admin.py代码

@admin.register(Car)
class CarAdmin(admin.ModelAdmin):
    list_display = ('manufacturer', 'model', 'model_year', 'mileage', 'status', 'date_added', 'price', 'seller')
    list_filter = ('manufacturer', 'status', 'transmission')
    fieldsets = (
        ('General information', {
            'fields': ('car_images', 'manufacturer', 'model',
            'model_year', 'price', 'vin', 'mileage', 'transmission', 'engine_displacement', 
            'forced_induction', 'drivetrain', 'description', 'id',)
        }),
       ('Availability', {
            'fields': ('status', 'seller')
        }),
    )

这是TemplateError

Template error:
In template C:\Users\USER\Envs\torque\lib\site-packages\django\contrib\admin\templates\admin\base.html, error at line 62
   int() argument must be a string, a bytes-like object or a number, not 'Car'
   52 :             {% endblock %}
   53 :         </div>
   54 :         {% endif %}
   55 :         {% endblock %}
   56 :         {% block nav-global %}{% endblock %}
   57 :     </div>
   58 :     <!-- END Header -->
   59 :     {% block breadcrumbs %}
   60 :     <div class="breadcrumbs">
   61 :     <a href="{% url 'admin:index' %}">{% trans 'Home' %}</a>
   62 :     {% if title %} &rsaquo; {{ title }}{% endif %}
   63 :     </div>
   64 :     {% endblock %}
   65 :     {% endif %}
   66 : 
   67 :     {% block messages %}
   68 :         {% if messages %}
   69 :         <ul class="messagelist">{% for message in messages %}
   70 :           <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message|capfirst }}</li>
   71 :         {% endfor %}</ul>
   72 :         {% endif %}

...
During handling of the above exception ('manufacturer'), another exception occurred:
...

Exception Type: TypeError at /admin/showroom/car/
Exception Value: int() argument must be a string, a bytes-like object or a number, not 'Car'


0 个答案:

没有答案