在Django中保存带有外键的对象时遇到问题



这是我在论坛上的第一个问题。

我是Django的新手,进展缓慢。我现在正在编写的代码在将对象保存为外键引用时抛出错误。

这些是我的模型:

# models.py 
class Make(models.Model):
name_text = models.CharField(max_length=100)
def __str__(self):
return self.name_text

class Autos(models.Model):
nickname = models.CharField(max_length=100)
make = models.ForeignKey(Make, on_delete=CASCADE)
milage = models.IntegerField(default=0)
comments = models.CharField(max_length=1000)

将数据发送给视图的HTML表单

<form action="{% url 'auto:addauto' %}" method="post">
{% csrf_token %}
<p>
<label for="nickname">Nickname : </label>
<input type="text" name="nickname" id="nickname" value="{{ autodetails.nickname }}">
</p>
<p>
<label for="make">Make : </label>
<select name="make" id="make">
{% for item in make %}
<option value="{{ item.name_text }} ">{{ item }}</option>
{% endfor %}
</select>
</p>
<p>
<label for="milage">Milage : </label>
<input type="text" name="milage" id="milage" value="{{ autodetails.milage }}">
</p>
<p>
<label for="comments">Comments : </label>
<input type="textarea" name="comments" id="comments" value="{{ autodetails.comments }}">
</p>

<input type="submit" name = "buttonsubmit" value="Add">
</form>

查看问题所在:

# views.py 
def addauto(request):
if request.method == 'POST':
obj = Autos()
obj.nickname = request.POST['nickname']
name_text1 = request.POST.get('make')
make = Make.objects.get(name_text = name_text1)
obj.make = make
obj.milage = request.POST['milage']
obj.comments = request.POST['comments']
obj.save()
return redirect('/autos/listautos')
else:
make = Make.objects.all()
context = {
'make': make
}
return render(request, 'autos/add_auto.html', context)

以上代码适用于:

make = Make.objects.filter(name_text = "some value")

我也试过下面的代码,但没有运气:

name_text1 = request.POST.get('make')
make = Make.objects.filter(name_text = name_text1)
obj.make = make

对于objects.get(),这是错误跟踪:

internal Server Error: /autos/addauto
Traceback (most recent call last):
File "/home/harsha/Projects/django3/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/home/harsha/Projects/django3/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/harsha/Projects/django-projects/mysite/autos/views.py", line 22, in addauto
make = Make.objects.get(name_text = name_text1)
File "/home/harsha/Projects/django3/lib/python3.8/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/harsha/Projects/django3/lib/python3.8/site-packages/django/db/models/query.py", line 435, in get
raise self.model.DoesNotExist(
autos.models.Make.DoesNotExist: Make matching query does not exist.

根据我现有的知识,我尝试了所有我能想到的——看起来我没有看到问题。如有任何帮助,不胜感激。

我能够解决这个问题,下面是我的解决方案。

I was passing "item.name_text" instead of "item.id" for the "value" of the dropdown
<p>
<label for="make">Make : </label>
<select name="make" id="make">
{% for item in make %}
**<option value="{{ item.id }} ">{{ item }}</option>**
{% endfor %}
</select>
</p>

更新后的视图

make = Make.objects.get(pk = request.POST.get('make'))
obj.make = make

相关内容

  • 没有找到相关文章

最新更新