【发布时间】:2020-09-09 06:30:23
【问题描述】:
我有这样的模型:
from django.db import models
class Article (models.Model):
author = models.ForeignKey('Users.User')
title = models.CharField()
text = models.TextField()
stars = [
models.IntegerField(), # 0 stars
models.IntegerField(), # 1 stars
models.IntegerField(), # 2 stars
models.IntegerField(), # 3 stars
models.IntegerField(), # 4 stars
models.IntegerField(), # 5 stars
]
但问题是星星字段不会在 000x_initials.py 中被识别。
我该怎么办?
我想这样使用它:
if request.method == 'POST':
post_data = request.post
star = int(post_data['star'])
article.stars[star] += 1
article.save()
【问题讨论】:
-
如果您使用的是 PostgreSQL,那么您可以使用数组字段docs.djangoproject.com/en/3.1/ref/contrib/postgres/fields
-
我不是。我正在使用 MySQL。
标签: python python-3.x django django-models django-3.1