为什么它不起作用?
models.py:
class Car(models.Model):
user = models.OneToOneField(User)
views.py:
def add_car(request, model):
new_car = Car(owner=request.user.id, model=model)
new_car.save()
错误:
ValueError at /add/
Cannot assign "7L": "Car.user" must be a "User" instance.
什么是7L
?
我的用户ID为7,当我写print request.user.id
时,它会让我7。
答案 0 :(得分:3)
您应该设置为Car.user整个模型,而不仅仅是id。
new_car = Car(owner=request.user, model=model)